从C#代码后面绑定DataGridTextColumn颜色

时间:2012-10-25 18:23:10

标签: c# wpf binding datagrid

WPF - help converting XAML binding expression to codebehind类似

我正在尝试使用Binding来更改DataGridTextColumn中某些元素的颜色。因为我在单独的选项卡中需要任意数量的DataGrids,所以我在代码隐藏中迭代地创建它们。这是我创建列的代码:

// 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;

3 个答案:

答案 0 :(得分:1)

解决方法:

在资源中创建一个样式,如下所示:

<Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">
  <Setter Property="Foreground" Value="{Binding TextColor}" />
</Style>

并在ElementStyle的{​​{1}}属性中设置此样式,在代码中应该是这样的:

DataGridColumn

column = new DataGridTextColumn(); column.Style = this.FindResource("MyStyle") as Style; 直接在列的内容中起作用的原因(即显示值的ElementStyle

答案 1 :(得分:1)

它也可以通过代码来实现,没有风格。 重要的一点是明确设置绑定源(在下面的代码中查找)为目标属性设置的新绑定表达式

在此示例中,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);                

}

答案 2 :(得分:0)

使用damascus中的解决方法,但只能使用后面的代码:

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;