Fallingsappy
Fallingsappy

Reputation: 303

UI Element is not updated depending on the DataTrigger bound to the value

I have viewModel

public class Measurement : INotifyPropertyChanged
{
    private bool _processed;
    public bool Processed
    {
        get
        {
            if ( .... ) return true;
            
            return false;
        }
        set
        {
            _processed = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Processed"));
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
            PropertyChanged?.Invoke(this, e);
    }
}

XAML:

<DataGrid 
  ItemsSource="{Binding CurrentSeries.MeasurementsSeries, 
  ElementName=AppMainWindow,Mode=TwoWay,NotifyOnSourceUpdated=True, 
  UpdateSourceTrigger=PropertyChanged}">
    <DataGrid.Columns>
      <DataGridTemplateColumn Width="45" Header="Status">
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <Grid>
              <TextBlock x:Name="PhotosStatusToolTip"
                ToolTip="Радиусы всех капель должны быть расчитаны, чтобы построить график."
                Height="25" Width="25" />
                  <ContentControl x:Name="PhotosStatus"
                    ToolTip="Радиусы всех капель должны быть расчитаны, чтобы построить график."
                    Content="{StaticResource Warning}"
                    Height="25" Width="25"
                    HorizontalAlignment="Center" />
            </Grid>
            <DataTemplate.Triggers>
              <DataTrigger Binding="{Binding Processed}" Value="true">
                <Setter Property="Content"
                  Value="{StaticResource Ok}"
                  TargetName="PhotosStatus" />
                <Setter Property="ToolTip"
                  Value="Радиус капли расчитан успешно."
                  TargetName="PhotosStatusToolTip" />
                <Setter Property="ToolTip"
                  Value="Радиус капли расчитан успешно."
                  TargetName="PhotosStatus" />
              </DataTrigger>
            </DataTemplate.Triggers>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>

I have two problems:

  1. binding to Processed property underlined with squiggly line and says: Cannot resolve symbol 'Processed'. But this binding seems to work fine. But would be nice to get rid of this message and I don't know how;
  2. I want Icon to change between Warning and Ok depending on Processed property, but data triggers seems to be firing only once on collection population. When Processed property changes it value, icon remains the same. How to achieve this behaviour?

I've already tried to add second data trigger for case when Processed is false, but this doesn't seems to help.

Upvotes: 0

Views: 32

Answers (1)

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 12007

Having a property value calculated as you indicated and providing a setter does not fit together. The expectation for a property is that whenever you set the value, the getter will return the set value which is not the case if you calculate the value in the getter.

So either the Processed getter should always return the value of _processed. Or you should remove the setter and add a OnPropertyChanged(new PropertyChangedEventArgs("Processed")); wherever you change anything influcencing the calculated value.

E.g.

public bool Processed
{
    get
    {
        if (X > 0 && Y > 0) return true;
        
        return false;
    }
}

public int X
{
    get { return _x; }
    set
    {
        _x = value;
        OnPropertyChanged(new PropertyChangedEventArgs(namepf(X)));
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(Processed)));
    }
}


public int Y
{
    get { return _y; }
    set
    {
        _y = value;
        OnPropertyChanged(new PropertyChangedEventArgs(namepf(Y)));
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(Processed)));
    }
}

Upvotes: 1

Related Questions