WPF数据网格行标头数据绑定

WPF数据网格行标头数据绑定,wpf,data-binding,datagrid,datatable,row,Wpf,Data Binding,Datagrid,Datatable,Row,我有一个DataGrid,绑定到一个DataTable。我想在行标题中显示文本,以实现如下目的: Col0 Col1 Col2 Col3 Table | 1 | 3 | 5 | 6 | Chair | 3 | 2 | 1 | 8 | 这可能吗?如果可能的话,我该怎么做?尝试设置行标题模板,如下所示 <DataGrid> &l

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

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

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

尝试设置行标题模板,如下所示

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

        //your stuff
</DataGrid>

不明飞行物
//你的东西

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

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


然后它几乎可以工作了。问题是,这会导致每一行都有相同的行标题。

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

这对我很有用:

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

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

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

{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                  AncestorType={x:Type DataGridRow}}, 
                                  Path=Item.Row.Header}
稍微翻了翻,发现在
Item.Row.Header
路径中,路径从DataGridRow开始,
Item
带您到DataGridView,而
带您到DataRow


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

2种方法可以做到这一点,上一个示例几乎拥有它,但是绑定将无法解析属性,因为表达式缺少“DataContext”


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

你是说RowHeader,还是说ColumnHeader?好的,当我写你的属性时,我的意思是你可以将RowHeader绑定到你添加到网格中的单行的任何属性;请随意提供您的一段代码,也许我可以进一步帮助您!请注意,路径实际上是
item.propertyname
。如果有人告诉我如何找出运行时的datacontext中有哪些属性可用,这将对我有很大帮助。您也可以使用
Item[0]
获取第一列,而不是指定名称并使用此答案隐藏第一列
{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                  AncestorType={x:Type DataGridRow}}, 
                                  Path=Item.Row.Header}
<DataGrid>
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DataContext.YourProperty}"></TextBlock>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>

        <!--your stuff-->
</DataGrid>
<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>
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;
    }
}