【问题标题】:INotifyPropertyChanged Binding to DataTemplate in GridColumnINotifyPropertyChanged 绑定到 GridColumn 中的 DataTemplate
【发布时间】:2012-12-07 05:19:37
【问题描述】:

我已成功地将 DataGrid 中的一列绑定到一个可观察的集合,其中的对象实现了 INotifyPropertyChanged 接口:

这是 xaml:

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

以及objects类中的属性:

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

但在另一列中,我使用的是模板:

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

矩形的 Fill 属性绑定到“计算”属性:

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

其他一些改变StapelStatus值的属性设置器正在调用

OnPropertyChanged("StatusColor"); 

我认为这足以改变网格列中矩形颜色的颜色。但不幸的是,当StapelStatus 被更改并且OnPropertyChanged("StatusColor") 被调用时,网格并没有反映这种变化。我想我必须以某种方式更改DataTemplate 中的绑定。有人可以给我一个建议吗?

【问题讨论】:

  • 为什么StatusColor绑定的路径中有Data.
  • 坦率地说,我不知道为什么。我在一个工作示例中发现了这一点。如果我删除数据。该值根本不会出现在 DataTemplate 中。
  • 您确定 DataTemplate 的 DataContext 正确吗? Name和StatusColor属性是同一个类吗?
  • 另一个提示:您可以使用静态 System.Windows.Media.Brushes 类而不是创建新画笔(Brushes.CornflowerBlueBrushes.DarkOrange
  • 这意味着那里没有绑定任何东西。您的代码中存在一些绑定问题。可能是您没有正确设置上下文。

标签: c# wpf xaml gridview inotifypropertychanged


【解决方案1】:

这行得通吗?

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");
    }
}

【讨论】:

  • 嗨 Snurre,感谢您的建议,但我的行为完全相同。将执行带有“OnPropertyChanged("StatusColor")”的 StatusColor 设置器,但网格不反映更改。
【解决方案2】:

这确实是 XAML 中的一个绑定问题。不幸的是,我没有在 GridColumn 中设置 FieldName 属性。我认为 FieldName 仅用于没有任何模板的普通 GridColumns。

列的工作 XAML 下方:

<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>

【讨论】:

    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 2013-06-25
    • 2013-05-29
    • 2011-11-27
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多