【问题标题】:Is it possible to use a converter within a style?是否可以在样式中使用转换器?
【发布时间】:2010-09-27 14:20:01
【问题描述】:

是否可以在样式中使用转换器?例如,我正在尝试创建一个样式化的TextBlock,其文本根据TextBlockActualHeight 属性调整大小。调整大小将通过转换器完成。

【问题讨论】:

    标签: c# wpf xaml styles converter


    【解决方案1】:

    我设法通过以下方式获得了类似的工作:

    <Setter Property="Text">
      <Setter.Value>
        <Binding Path="CompanyName">
          <Binding.Converter>
            <conv:UppercaseConverter/>
          </Binding.Converter>
        </Binding>
      </Setter.Value>
    </Setter>
    

    希望它也对你有用。

    亚恩

    PS - CompanyName 是我将文本块绑定到的实际 ViewModel 属性的名称

    【讨论】:

      【解决方案2】:

      是的,这是可能的。例如:

      <Style TargetType="TextBlock">
          <Setter Property="FontSize">
              <Setter.Value>
                  <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}">
                      <Binding.Converter>
                          <MyConverter/>
                      </Binding.Converter>
                  </Binding>
              </Setter.Value>
          </Setter>
      </Style>
      

      根据您的具体情况,您可能还可以使用更简洁的:

      <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}"/>
      </Style>
      

      【讨论】:

        猜你喜欢
        • 2015-04-01
        • 2019-01-20
        • 1970-01-01
        • 2015-05-02
        • 1970-01-01
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多