WPF DataGrid RowHeader 数据绑定

问题描述 投票:0回答:6

我有一个 DataGrid,绑定到一个 DataTable。我想在 RowHeader 中显示文本,以实现这样的效果:

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

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

wpf data-binding datagrid datatable row
6个回答
40
投票

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

这对我有用:

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

诀窍是找到祖先

DataGridRow
,然后将
TextBlock.Text
属性绑定到您关心的 Item 属性,在本例中为
Header
(在 XAML 中可能比英语更容易说)。

然后在.xaml.cs中:

ui_dataGrid.ItemsSource = dataSource.Rows;

注意每个

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


6
投票

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

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

2
投票

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

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

稍微查了一下,发现在

Item.Row.Header
路径中,路径从DataGridRow开始,
Item
带你到DataGridView
Row
带你到DataRow

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


2
投票

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

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

        //your stuff
</DataGrid>

0
投票

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

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

然后就差不多可以用了。问题是这会导致每行都有相同的行标题。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.