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