简体   繁体   中英

Is it possible to use a converter within a style?

Is it possible to use a converter within a style? For instance I am trying to create a styled TextBlock whose text resizes based on the ActualHeight property of the TextBlock . The resizing would be done via a converter.

Yes, this is possible. For example:

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

Depending on your exact scenario, you might also be able to use the more succinct:

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

I managed to get something similar to work by using:

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

Hope it works for you too.

Yann

PS - CompanyName is the name of the actual ViewModel property I was binding the textblock to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM