【问题标题】:C# WPF DataGrid Bind Row background to Property found in DataRowC# WPF DataGrid 将行背景绑定到 DataRow 中的属性
【发布时间】:2015-02-05 19:13:26
【问题描述】:

我需要将 DataRow 的背景绑定到附加到 DataRow 的对象的属性上。我所做的是:

  • 我已扩展 DataRow 类,使其具有 [object] 类型的“Tag”属性。
  • 例子:

    myDataTable.Rows.Cast<ExtendedDataRow>().ToList(){r => {
        r.Tag = Brushes.Green;
    });
    

    所以基本上对于每一行,都有一个标签属性,它是一个画笔,绿色。我需要将我的 DataTable 绑定到这个数据集,并将每一行绑定到 Tag 属性。

    我尝试过的:

    <DataGrid ItemsSource="{Binding myDataTable}">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding Tag.Background}" />
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
    

    但是当我尝试绑定它时,它似乎没有“拾取”标签项目。我需要为此创建一个 ItemTemplate 吗? (我试过了,还是不行)

    注意:数据集绑定成功,在 ViewModel 的代码中,我可以看到每一行的 Tag 项都已填充。

    提前致谢

    编辑:有人要求查看我的 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; }
    }
    

    编辑 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);
    

    【问题讨论】:

    • 帮帮我,ExtendedDataRow 是如何创建的?
    • 我创建了一个 ExtendedDataTable 类,它使用 ExtendedDataRow 类作为它的 RowType。示例代码见我对问题的回复
    • 示例代码在哪里?
    • 不好意思刚加了,ExtendedDataTable怎么用。请查看我的编辑

    标签: c# wpf binding datagrid


    【解决方案1】:

    我按照预期做了每一件事,就像你所做的一样。

    我通过查看输出窗口注意到了这个问题:

          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 是一种内部封装的方式,称为 DataRowView

    快速浏览msdn - DataRowView.Row

    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>
    

    【讨论】:

    • 谢谢,当绑定到 Row.Tag 时,它就像一个魅力!我学会了如何使用输出窗口......谢谢:D
    • 你知道如何使用 snoop ..吗?
    • 我不知道它是什么,也不知道如何使用它。但我现在要阅读它..
    • 抱歉,新手 ;)
    • 4 年后,我从未回复。先生,您通过介绍 snoop 为我节省了很多时间。在进行 WPF 开发时,它几乎是 must have
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2014-10-05
    • 2021-12-26
    相关资源
    最近更新 更多