Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#codebehind绑定DataGridTextColumn颜色_C#_Wpf_Binding_Datagrid - Fatal编程技术网

从C#codebehind绑定DataGridTextColumn颜色

从C#codebehind绑定DataGridTextColumn颜色,c#,wpf,binding,datagrid,C#,Wpf,Binding,Datagrid,近似 我试图使用绑定来更改DataGridTextColumn中某些元素的颜色。因为我需要在单独的选项卡中使用任意数量的数据网格,所以我在codebehind中以迭代方式创建它们。以下是我创建列的代码: // create a value column column = new DataGridTextColumn(); column.Binding = new Binding("Value"); BindingOperations.SetBinding(column, DataGridText

近似

我试图使用绑定来更改DataGridTextColumn中某些元素的颜色。因为我需要在单独的选项卡中使用任意数量的数据网格,所以我在codebehind中以迭代方式创建它们。以下是我创建列的代码:

// create a value column
column = new DataGridTextColumn();
column.Binding = new Binding("Value");
BindingOperations.SetBinding(column, DataGridTextColumn.ForegroundProperty, new Binding("TextColor"));
listGrid.Columns.Add(column);
值绑定工作正常,但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实现InotifyProperty更改如下:

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中的断点,值文本在显示中更新。但是,跨过“TextColor”行不会激活TextColor getter中的断点,并且文本颜色不会更改。知道这是怎么回事吗

编辑:这是答案,感谢大马士革。(我本想在他的回答中添加这一点,但这不会正确格式化我的代码。)我将其添加到XAML文件中:

<Window.Resources>
    <Style x:Key="BoundColorStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="{Binding TextColor}" />
    </Style>
</Window.Resources>
解决此问题的方法:

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

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

原因是
ElementStyle
直接作用于列的内容(即
TextBlock
显示值)

它可以通过代码隐藏实现,也可以不使用样式。 重要的一点是为为目标属性设置的新绑定表达式显式设置绑定源(查看下面的代码)

在此示例中,DataGrid生成动态列,并与ColloActionViewSource(即cycleDataview)的对象绑定,cycleDataview的源是cycleRecord的ObservableCollection对象

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

谢谢我发布了我在上面的问题中添加的确切代码。
// 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;