WPF DataTrigger Setter method binding

493 views Asked by At

I want to display different cell background color based on the value being changed by using Style. Here is my code. My data structure implements the INotifyPropertyChanged, whenever a property is changed, it will raise the notification (e.g. HasChange). So I assume that when HasChange is changed, it will call MyDummy converter, which always returns true, then the setting method will be called and MyConverter will work out what color to paint for the background.

When the application starts, it works fine and displays the correct color. However, when I change any cell data, MyDummy is executed, but MyConverter never gets called again. Any idea? (Please ignore the RowData.Row, they are part of DevExpress controls for datarow). Thanks.

SOLUTION: is to use Data instead of DataRow.Row

    <local:MyColorConverter x:Key="MyConverter"/>
    <local:MyDummy x:Key="MyDummy"/>
    <Style TargetType="dxg:CellContentPresenter" x:Key="cellstyple">
        <Style.Triggers>
            <DataTrigger Value="true" Binding="{Binding RowData.Row.HasChange, Converter={StaticResource MyDummy}}">
                <Setter Property="Background">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource MyConverter}">
                            <Binding Path="RowData.Row"/>
                            <Binding Path="Column.FieldName"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

Data structure I used

public class MyStr : INotifyPropertyChanged
{
    private string _Name;
    public String Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            HasChange = true;
        }
    }
    public String City { get; set; }
    private bool _hasChange;
    public bool HasChange
    {
        get
        { return _hasChange; }
        set
        {
            _hasChange = value;
            NotifyChange("HasChange");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyChange(String name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

}
0

There are 0 answers