[英]Binding DataGridTextColumn color from C# codebehind
類似於WPF - 幫助將 XAML 綁定表達式轉換為代碼隱藏
我正在嘗試使用綁定來更改 DataGridTextColumn 中某些元素的顏色。 因為我需要在單獨的選項卡中使用任意數量的 DataGrid,所以我在代碼隱藏中迭代地創建它們。 這是我創建列的代碼:
// create a value column
column = new DataGridTextColumn();
column.Binding = new Binding("Value");
BindingOperations.SetBinding(column, DataGridTextColumn.ForegroundProperty, new Binding("TextColor"));
listGrid.Columns.Add(column);
Value 綁定工作正常,但 TextColor 屬性的 getter 永遠不會被調用。
網格的 ItemsSource 屬性設置為 VariableWatcher 對象列表,以下是它的一些屬性:
public bool Value
{
get { return _variable.Value; }
}
// used to set DataGridTextColumn.Foreground
public Brush TextColor
{
get
{
Color brushColor;
if (_valueChanged)
brushColor = Color.FromRgb(255, 0, 0);
else
brushColor = Color.FromRgb(0, 0, 0);
return new SolidColorBrush(brushColor);
}
}
VariableWatcher 實現 INotifyPropertyChanged 如下:
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
在 VariableWatcher 的方法之一中,我有以下幾行:
_valueChanged = true;
NotifyPropertyChanged("Value");
NotifyPropertyChanged("TextColor");
跨過“Value”行會激活 Value getter 中的斷點,並且顯示中的 Value 文本會更新。 但是,跨過“TextColor”行不會激活 TextColor getter 中的斷點,並且文本顏色不會改變。 知道這里發生了什么嗎?
編輯:這是答案,感謝大馬士革。 (我會將其放在對他的回答的評論中,但它不會正確格式化我的代碼。)我將其添加到 XAML 文件中:
<Window.Resources>
<Style x:Key="BoundColorStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding TextColor}" />
</Style>
</Window.Resources>
在 C# 代碼中,我將 BindingOperations 行替換為
column.ElementStyle = this.FindResource("BoundColorStyle") as Style;
解決方法:
在您的資源中創建一個樣式,如下所示:
<Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding TextColor}" />
</Style>
並在您的DataGridColumn
的ElementStyle
屬性中設置此樣式,在您的代碼中應該是這樣的:
column = new DataGridTextColumn();
column.Style = this.FindResource("MyStyle") as Style;
原因是ElementStyle
直接在列的內容中工作(即顯示值的TextBlock
)
它可以通過代碼隱藏以及沒有樣式來實現。 重要的一點是為目標屬性設置的新綁定表達式顯式設置綁定源(查看下面的代碼) 。
在本例中,DataGrid 生成動態列,並以ColloectionViewSource 對象(即cycleDataview)為界,cycleDataview 的源是ObservableCollection 對象cycleRecord。
// Create view source
this.cycleDataview = new CollectionViewSource();
this.cycleDataview.Source = this.cycleRecords;
// Set Item Source to data grid
this.DataGridCycleData.ItemsSource = this.cycleDataview.View;
// Generate Columns for datagrid
var columns = this.cycleRecords.First().CyclePartCols.Select((x, i) => new {PreDescriptor = x.PreDescriptor, Index = i }).ToArray();
foreach (var column in columns)
{
Binding binding = new Binding(string.Format("CyclePartCols[{0}].PartValue", column.Index));
Binding bindingColor = new Binding(string.Format("CyclePartCols[{0}].TextColor", column.Index));
**bindingColor.Source = this.cycleRecords;** // Binding source is required to set
DataGridTextColumn dgc = new DataGridTextColumn();
txtblckCol.Text = column.PreDescriptor;
dgc.Header = txtblckCol;
dgc.Binding = binding;
this.DataGridCycleData.Columns.Add(dgc);
BindingOperations.SetBinding(dgc, DataGridTextColumn.ForegroundProperty, bindingColor);
}
使用大馬士革的解決方法,但僅限於背后的代碼:
var myStyle = new Style
{
TargetType = typeof(TextBlock)
};
textColumnStyleExt.Setters.Add(new Setter(TextBlock.ForegroundProperty, new Binding("TextColor"));
column = new DataGridTextColumn();
column.Binding = new Binding("Value");
column.ElementStyle = myStyle;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.