WPF DataGrid RowHeader 数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4791929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:31:45  来源:igfitidea点击:

WPF DataGrid RowHeader databinding

wpfdata-bindingdatagriddatatablerow

提问by eriksmith200

I have a DataGrid, bound to a DataTable. I want to display text in the RowHeader, to achieve something like this:

我有一个 DataGrid,绑定到一个 DataTable。我想在 RowHeader 中显示文本,以实现如下目标:

         Col0      Col1      Col2      Col3
Table |    1    |    3    |    5    |    6    |
Chair |    3    |    2    |    1    |    8    |

Is this possible and if so, how can I do this?

这可能吗,如果可以,我该怎么做?

回答by markmuetz

I tried both answers, and neither worked for me. Essentially what I had to do was mix them together.

我尝试了两种答案,但都不适合我。基本上我要做的就是将它们混合在一起。

This works for me:

这对我有用:

<DataGrid name="ui_dataGrid>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                      AncestorType={x:Type DataGridRow}}, 
                                      Path=Item.Header}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

The trick is to find the ancestor DataGridRow, then Bind the TextBlock.Textattribute to its Item's property that you care about, in this case Header(easier said in XAML than English maybe).

诀窍是找到祖先DataGridRow,然后将TextBlock.Text属性绑定到您关心的 Item 属性,在这种情况下Header(在 XAML 中可能比英语更容易说)。

Then in the .xaml.cs:

然后在 .xaml.cs 中:

ui_dataGrid.ItemsSource = dataSource.Rows;

N.B. Each Rowobject has a Headerproperty which is what I'm binding too.

注意每个Row对象都有一个Header属性,这也是我要绑定的。

回答by denis morozov

2 ways to do it, the prev example almost had it but the binding would fail to resolve the property because the expression was missing "DataContext."

有 2 种方法可以做到,上一个示例几乎已经有了它,但是由于表达式缺少“DataContext”,因此绑定将无法解析该属性。

<DataGrid>
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DataContext.YourProperty}"></TextBlock>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>

        <!--your stuff-->
</DataGrid>

2nd way to do it is to create a converter to get the binding, parse it in the converter and spit out whatever string value you want:

第二种方法是创建一个转换器来获取绑定,在转换器中解析它并吐出你想要的任何字符串值:

<Views:DataGridRowDataContextToRowHeaderValueConverter x:Key="toRowHeaderValue"/>

<DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                       AncestorType={x:Type DataGridRow}},
                       Converter={StaticResource toRowHeaderValue}}"/>
        </DataTemplate>
</DataGrid.RowHeaderTemplate>

Sample converter code:

示例转换器代码:

public class DataGridRowDataContextToRowHeaderValueConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, 
                           CultureInfo culture)
    {     
        var dataGridRow = (DataGridRow) value;
        var row = (GridModelExtensions.HourRow) dataGridRow.DataContext;
        return row.Days[0].Hour;
    }
}

回答by michele

Try to set the rowheader template, something like this

尝试设置行标题模板,像这样

<DataGrid>
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding YourProperty}"></TextBlock>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>uff

        //your stuff
</DataGrid>

回答by Bryan Johnson

Just FYI, if you bind directly to a data table, then this binding text worked for me:

仅供参考,如果您直接绑定到数据表,那么此绑定文本对我有用:

{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                  AncestorType={x:Type DataGridRow}}, 
                                  Path=Item.Row.Header}

Poked around a little bit and found that in the Item.Row.Headerpath, the path starts at the DataGridRow, the Itemtakes you to the DataGridView, and the Rowtakes you to the DataRow.

稍微翻了一下,发现在Item.Row.Header路径中,路径从DataGridRow开始,Item将您带到DataGridView,然后Row将您带到DataRow

Again, if you bind directly to the data table.

同样,如果您直接绑定到数据表。

回答by eriksmith200

@michele: If I modify the Binding to:

@michele:如果我将绑定修改为:

<TextBlock Text="{Binding DataContext.YourProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/> 

then it almost works. The problem is that this results in the same row header for each row.

那么它几乎可以工作。问题是这会导致每行具有相同的行标题。

回答by user1680868

You were almost there just change the AncestorType to DataGridRowinstead of DataGridthen the row headers will be different for each row e.g.

您几乎在那里只是将 AncestorType 更改为DataGridRow而不是DataGrid那么每行的行标题将不同,例如