繁体   English   中英

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

[英]Programatically bind setter to custom dependency property

我有自定义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;

尝试在触发器之外向样式添加一个setter,这将初始化Background的值。

style.Setters.Add(new Setter(BackgroundProperty, Brushes.White));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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