Reputation: 11003
I have an ObservableCollection<MyClass>
. And MyClass
has a property called IsColored
I created a ListView, which should will color all the rows who have the property IsColored to true.
<ListView ItemsSource="{Binding MyClassList}">
<hControls:ListView.Style>
<Style TargetType="ListView">
<Setter Property="Foreground" Value="Blue" />
</Style>
</hControls:ListView.Style>
...
</ListView>
To satisfy the condition of Colors to my ListView I should add a DataTrigger
<Style.Triggers>
<Trigger Property="IsColored" Value="True">
<Setter Property="Foreground" Value="Blue" />
</Trigger>
</Style.Triggers>
But the problem is that IsColored is not recognized.
How can add a Binding to that property so I can access to it from the DataTrigger ?
Upvotes: 0
Views: 95
Reputation: 22702
In first case you using the regular Style.Trigger
, the DataTrigger
using like this:
<DataTrigger Bindind="{Binding Path=IsColored}" Value="True">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
About this error:
Cannot resolve property 'IsColored' in data context of type MyNameSpace.MyUserControl
It tells, what you don't correctly setting the DataContext
.
Upvotes: 0
Reputation: 69987
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>
Upvotes: 3