[英]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.