Is it possible to use a converter within a style?

18,273

Solution 1

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>

Solution 2

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

Share:
18,273

Related videos on Youtube

Dave Clemmer
Author by

Dave Clemmer

I enjoy coding like an excellent beer. My particular passion and experience lies in the realm of modeling and code generation. In the late 80s and early 90s, I was involved in early modeling and code generation tools that reached the marketplace, including a tool that modeled FORTRAN programs and generated FORTRAN for parallel supercomputer architectures, and a tool that managed Shlaer-Mellor models and generated C++ code. Over the years, I have applied Shlaer-Mellor, UML, and custom modeling and various code generation techniques to greatly benefit the development of enterprise applications. My current passion and endeavor is to foster the evolution of model oriented development. In particular, I am passionate about evolving the Mo+ model oriented programming language and related model driven development tools, with as much community input as possible to achieve higher levels in the ability to create and maintain code. The open source site is at moplus.codeplex.com, and the Mo+ membership site is at modelorientedplus.com.

Updated on April 19, 2022

Comments

  • Dave Clemmer
    Dave Clemmer about 2 years

    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.