marcel
marcel

Reputation: 3272

Replace a text-converter dynamically

I have a textblock of a user-control which uses a converter to display figures. This converter gets loaded in a ResourceDictionary at the start of the application. Now I'd like to exchange this converter with another one, to display a different format, depending on a parameter in the app-settings. Unfortunatley using just a trigger doesn't work. Is it possible to load a converter dynamically into the user-control, and put a reference on it from a textblock?

Edit: Here's my trigger attempt:

<TextBlock>
       <TextBlock.Style>
                <Style>
                     <Style.Triggers>
                      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mycontrol}, AncestorLevel=1}, Path=mode}" Value="0">
                            <Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type cg:Fader}, AncestorLevel=1}, Path=Figure, Converter={StaticResource ConverterA}}"/>
                      </DataTrigger>
                      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mycontrol}, AncestorLevel=1}, Path=mode}" Value="1">
                            <Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type cg:Fader}, AncestorLevel=1}, Path=Figure, Converter={StaticResource ConverterB}}"/>
                      </DataTrigger>
                      </Style.Triggers>
                 </Style>
         </TextBlock.Style>
</TextBlock>

Upvotes: 0

Views: 45

Answers (1)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

The reason why triggers are not working in your example is in bindings. Overall Style + DataTriggers are perfectly suitable for dynamic template selection.

Upvotes: 1

Related Questions