I'm not sure why @Anatoliy gave up so quickly and deleted his answer, because he was right... you do need to use a DataTrigger
:
<Style.Triggers>
<DataTrigger Binding="{Binding IsColored}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
You commented to say that you got this error when trying this code:
Cannot resolve property 'IsColored' in data context of type MyNameSpace.MyUserControl
This just means that your Style
does not have access to the items of the ListView
. Looking at your code, it seems as though you are trying to define a Style
for a ListViewItem
, but within a ListView Style
. Instead, you need to use the ListView.ItemContainerStyle
Property to apply the Style
to the individual items. Try this instead:
<ListView ItemsSource="{Binding MyClassList}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsColored}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>