[英]C# WPF DataGrid Bind Row background to Property found in DataRow
I need to bind a background of a DataRow, to a property of an object attached to the DataRow. 我需要将DataRow的背景绑定到附加到DataRow的对象的属性。 What I have done is:
我所做的是:
Example: 例:
myDataTable.Rows.Cast<ExtendedDataRow>().ToList(){r => {
r.Tag = Brushes.Green;
});
So basically for every Row, there is a Tag property which is a Brush, Green. 因此,基本上每个行都有一个Tag属性,即Brush(画笔),Green(绿色)。 I need to bind my DataTable to this dataset, and bind each row to the Tag property.
我需要将DataTable绑定到该数据集,并将每一行绑定到Tag属性。
What I have tried: 我尝试过的
<DataGrid ItemsSource="{Binding myDataTable}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Tag.Background}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
But it does not seem to 'pick up' the Tag item when I am trying to bind to it. 但是当我试图绑定到Tag项时,它似乎并没有“拾取” Tag项。 Do I need to create an ItemTemplate for this?
我需要为此创建一个ItemTemplate吗? (I have tried it, also did not work)
(我尝试过,也没有用)
Note : The dataset is binding successfully, and in the code in the ViewModel I can see the Tag items for each row is populated.
注意 :数据集已成功绑定,在ViewModel的代码中,我可以看到每行的Tag项已填充。
Thanks in advance 提前致谢
EDIT : It has been requested to see how my ExtendedDataRow class is used:
编辑 :已被要求查看如何使用我的ExtendedDataRow类:
public class ExtendedDataTable : DataTable {
public ExtendedDataTable()
: base() {
}
public ExtendedDataTable(string tableName)
: base(tableName) {
}
public ExtendedDataTable(string tableName, string tableNamespace)
: base(tableName, tableNamespace) {
}
// Return the RowType as ExtendedDataRow instead of DataRow
protected override Type GetRowType() {
return typeof(ExtendedDataRow);
}
// Use the RowBuilder to return an ExtendedDataRow instead of DataRow
protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
return new ExtendedDataRow(builder);
}
}
public class ExtendedDataRow : DataRow {
public ExtendedDataRow()
: base(null) {
}
public ExtendedDataRow(DataRowBuilder rb)
: base(rb) {
}
// The tag object attached to the ExtendedDataRow
public object Tag { get; set; }
}
Edit 2 : To bind to the ExtendedDataTable instead of a normal DataTable, you have to fill a normal DataTable, and use its IDataReader to fill the dataset of the ExtendedDataTable:
编辑2 :要绑定到ExtendedDataTable而不是普通的DataTable,必须填充普通的DataTable,并使用其IDataReader填充ExtendedDataTable的数据集:
myDt = new ExtendedDataTable();
dt = new DataTable();
var dt = GetDataTable("SELECT * FROM SomeTable");
var reader = dt.DataSet.CreateDataReader(dt);
myDt.Load(reader);
I did every thing as expected , just like you've done . 就像你做的一样,我做了一切都符合预期。
I noticed the problem by looking in the output window : 我在输出窗口中发现了问题:
System.Windows.Data Error: 40 : BindingExpression path error: 'Tag' property not found on 'object' ''DataRowView' (HashCode=30296746)'. BindingExpression:Path=Tag; DataItem='DataRowView' (HashCode=30296746); target element is 'DataGridRow' (Name=''); target property is 'Background' (type 'Brush')
DataRow is some how internally wrapped with something called a DataRowView DataRow是一些内部封装有DataRowView的东西
A quick glance in msdn - DataRowView.Row 快速浏览msdn-DataRowView.Row
XAML : XAML:
<DataGrid CanUserAddRows="False" ItemsSource="{Binding Table}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Row.Tag, Mode=OneWay}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.