【问题标题】:WPF DataGrid RowHeader databindingWPF DataGrid RowHeader 数据绑定
【发布时间】:2011-06-15 02:00:46
【问题描述】:

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

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

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

【问题讨论】:

  • 你是说RowHeader,还是说ColumnHeader?

标签: wpf data-binding datagrid datatable row


【解决方案1】:

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

这对我有用:

<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 属性绑定到您关心的项目属性,在本例中为Header(在XAML 中说起来可能比英语更容易)。

然后在.xaml.cs中:

ui_dataGrid.ItemsSource = dataSource.Rows;

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

【讨论】:

  • 注意路径真的是item.propertyname如果有人能告诉我如何找出运行时数据上下文中可用的属性,那对我有很大帮助。
  • 您也可以使用Item[0] 获取第一列,而不是指定名称并使用此答案stackoverflow.com/a/16460193/5762332 隐藏第一列
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    尝试设置rowheader模板,像这样

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

    【讨论】:

      【解决方案4】:

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

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

      逛了一圈,发现Item.Row.Header路径中,路径从DataGridRow开始,Item带你到DataGridView,和Row 将您带到 DataRow

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

      【讨论】:

        【解决方案5】:

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

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

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

        【讨论】:

        • 好的,当我写 YourProperty 时,我的意思是您可以将 RowHeader 绑定到您添加到网格的单行的任何属性;随时提供您的一段代码,也许我可以进一步帮助您!
        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-06
          • 2012-08-17
          • 1970-01-01
          • 2011-03-22
          • 2017-05-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多