1
votes

In a MVVM project I want to apply the style "ReadOnlyTextBox" to a control only if a property exposed by the viewmodel is set to true. I thought I could use a trigger but unfortunately I learnt from here

Style Trigger to Apply another Style

and here

How to make Style.Triggers trigger a different named style to be applied

that I cannot change a style property inside a style definition. I'm looking for a simple alternative way, otherwise I will use the trigger for applying all the properties contained inside the style "ReadOnlyTextBox". I don't like this approach because it violates the DRY principle so I hope you can suggest something else.

Thanks Filippo

2

2 Answers

1
votes

If your not really forced to use Triggers, a pragmatic way could be using an IValueConverter that takes your ViewModel-Value as Input and returns the desired Style (smoewhat similar to DataTemplateSelectors) and then just do something like

<UserControl.Resources>
<prefix:StyleConverter x:Key="styleConverter"/>
</UserControl.Resources>
<!-- blabla -->
<TextBox Style="{Binding SomeCondition, Converter={StaticResource styleConverter}}"/>
<!-- blabla -->
0
votes

You can use style selector to apply the styles based on custom condition.

StyleSelector