以编程方式将setter绑定到自定义依赖项属性

时间:2015-07-29 07:09:39

标签: c# wpf dependency-properties

我有自定义DataGrid,并在OnAutoGeneratingColumn我根据列的类型创建样式。我有第一列DateTime,我不会改变,然后其余的,我想要检测,如果值超出一些限制。现在,如果值超出其限制,我想更改背景,我想也能够更改背景的颜色。所以我创建了一个依赖属性,在数据触发器中我想将颜色绑定到该属性。唯一的问题是,它不起作用。有什么想法吗?

我的依赖属性

public Color BiggerThanMaxBackgroundColor
{
    get { return (Color)GetValue(BiggerThanMaxBackgroundColorProperty); }
    set { SetValue(BiggerThanMaxBackgroundColorProperty, value); }
}

public static readonly DependencyProperty BiggerThanMaxBackgroundColorProperty =
        DependencyProperty.Register("BiggerThanMaxBackgroundColor", typeof(Color), typeof(MwiTableDataGrid), new PropertyMetadata(default(Color)));

OnAutoGeneratingColumn

中的样式设置
//Trigger to check if the number is higher than the top limit
DataTrigger higherTrigger = new DataTrigger();
higherTrigger.Binding = new Binding(columnHeaderName)
              {
                Converter = new MoreThanConverter(),
                ConverterParameter = SelectedDevices[this.Columns.Count - 1].maxValue
              };
higherTrigger.Value = "True";

binding = new Binding
              {
                Source = this,
                Path = new PropertyPath("BiggerThanMaxBackgroundColor")
              };
higherTrigger.Setters.Add(new Setter(BackgroundProperty, binding));

Style style = new Style(typeof(DataGridCell));
style.Triggers.Add(higherTrigger);

e.Column.CellStyle = style;

1 个答案:

答案 0 :(得分:0)

尝试在触发器外部的样式中添加一个setter,它将初始化Background的值。

style.Setters.Add(new Setter(BackgroundProperty, Brushes.White));
相关问题