Search code examples
wpfinotifypropertychangeddatatrigger

Why is this DataTrigger not working after a PropertyChanged?


The following triggers work almost as expected:

<Style.Triggers>
    <Trigger Value="True" Property="IsSelected">
        <Setter Property="Foreground" Value="White" />
    </Trigger>
    <DataTrigger Value="True" Binding="{Binding UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InUseConverter}}">
        <Setter Property="Foreground" Value="OrangeRed" />
    </DataTrigger>
</Style.Triggers>

After loading the view, the colors are correct.
Then I execute an async taks en when it has finished I give the propertychanged on the object that has the binding to my datagrid-row.
But why is the DataTrigger not fired (I have to refresh the view to see the effect)?

EDIT:
My problem is that I don't now which property I have to give the PropertyChanged.
Some details about the datagrid (Projects is an ObservableCollection):

DataGrid SelectedItem="{Binding Project}" ItemsSource="{Binding Projects}">

The property of object Project that the binding must use is:
Project.Variants[0].InUse

I tried also the triggers:

<DataTrigger Value="True" Binding="{Binding Path=Variants[0].InUse, Converter={StaticResource NotNullConverter}}">

<DataTrigger Value="True" Binding="{Binding Path=., Converter={StaticResource InUseConverter}}">

In the view model I have tried after Project.Variants[0].InUse = null;:

Project.OnPropertyChanged("InUse");
Project.Variants[0].OnPropertyChanged("InUse");
raisePropertyChanged("Project.Variants[0].InUse");
raisePropertyChanged("Variants[0].InUse");
raisePropertyChanged("Projects");
raisePropertyChanged("Project");
raisePropertyChanged("InUse");

Solution

  • At last it works using:

    <DataTrigger Value="True" Binding="{Binding Path=Variants[0].InUse, Converter={StaticResource NotNullConverter}}">
      <Setter Property="Foreground" Value="OrangeRed" />
    </DataTrigger>
    
    Project.Variants[0].OnPropertyChanged("InUse");