简体   繁体   中英

INotifyPropertyChanged Binding to DataTemplate in GridColumn

I have successfully bound a column in a DataGrid to an observable collection with objects implementing the INotifyPropertyChanged interface:

This is the xaml:

<dxg:GridColumn Name="Name" FieldName="Stapel" DisplayMemberBinding="{Binding Path=Name}" />

And the property in the objects class:

public string Name
    {
        get { return _name; }
        set
        {
            if (value == _name) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }

But in another column I am using a Template:

<dxg:GridColumn.CellTemplate>
    <DataTemplate>
        <StackPanel>
                <Rectangle Height="19" Width="19" Fill="{Binding Path=Data.StatusColor}"></Rectangle>
        </StackPanel>
    </DataTemplate>
</dxg:GridColumn.CellTemplate>

The Fill property of the rectangle is bound to a "calculated" property:

public SolidColorBrush StatusColor
{
    get
    {
        if (StapelStatus == StapelStatus.Neu)
        {
            return new SolidColorBrush(Colors.CornflowerBlue);
        }
        return new SolidColorBrush(Colors.DarkOrange);
    }
}

Some other property setters, which change the value of StapelStatus are calling

OnPropertyChanged("StatusColor"); 

I thought this would be enough to change also the color in the rectangle color in the grid column. But unfortunately when the StapelStatus is changed and OnPropertyChanged("StatusColor") is called the grid doesn't reflect this change. I guess that I have to change somehow the binding in the DataTemplate . Can someone please give me an advice?

Would this work?

public Whatever StapelStatus
{
    get { return _stapelStatus; }
    set
    {
        _stapelStatus = value;
        OnPropertyChanged("StapelStatus");
        StatusColor = value == StapelStatus.Neu ? new SolidColorBrush(Colors.CornflowerBlue) : new SolidColorBrush(Colors.DarkOrange);
    }
}

public Brush StatusColor
{
    get { return _statusColor; }
    set
    {
        _statusColor = value;
        OnPropertyChanged("StatusColor");
    }
}

It was indeed a binding problem in the XAML. Unfortunately I did not set the FieldName property in the GridColumn. I thought the FieldName is only for use in normal GridColumns which don't have any Template.

Below the working XAML for the column:

<dxg:GridColumn Header="" FieldName="StatusColor">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Rectangle RadiusX="19" RadiusY="19" Height="19" Width="19" Stroke="Black" Fill="{Binding Path=Value}"></Rectangle>
                </StackPanel>
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM