WPF DataGrid自定义行标题

编程入门 行业动态 更新时间:2024-10-22 21:25:29
本文介绍了WPF DataGrid自定义行标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用WPF/EntityFramework/MVVM开发数据库前端

I am working on a DB FrontEnd with WPF / EntityFramework / MVVM

现在,当我允许用户向数据网格(绑定到Observable集合)中添加数据时,我陷入了困境.

Now i got stuck when i allow the user to add data to a datagrid (which is bound to an Observable Collection).

我想要实现的,是获得像MS Access中那样的行标题:所以我的WPF DataGrid基本上应该像这样:

What i want to achieve, is to get a row header like in MS Access: So my WPF DataGrid should look like this basically:

是否可以将RowHeaderStyle绑定到RowState?例如:

Is there any way to bind the RowHeaderStyle to the RowState? For Example:

  • RowState.Editing:显示编辑图标
  • RowState.NewRow:显示星空
  • RowState.默认:显示默认行标题

到目前为止,我没有找到任何解决方案,但是我认为WPF应该足够强大才能完成这项工作.

I found no solution so far, but i think WPF should pe powerfull enough to get this job done.

谢谢!

推荐答案

简单.为 DataGrid 提供一个 RowHeaderStyle ,它可以根据 DataGridRow 的状态交换不同的 ContentTemplates .幸运的是, DataGridRow 是 DataGridRowHeader 的可视祖先,因此使用 RelativeSource 绑定到达该位置并获取相关属性: DataGridRow.IsEditing 和 DataGridRow.IsNewItem .

Simple. Give the DataGrid a RowHeaderStyle that swaps in different ContentTemplates depending on the state of the DataGridRow. Fortunately the DataGridRow is a visual ancestor of the DataGridRowHeader, so it's relatively simple to reach up there with a RelativeSource binding and get the values of the relevant properties: DataGridRow.IsEditing and DataGridRow.IsNewItem.

我将< Label> New</Label> 等用作您要使用的任何内容的任意替代.

I used <Label>New</Label> etc. as an arbitrary stand-in for whatever content you want to use.

<DataGrid ItemsSource="{Binding Rows}" > <DataGrid.RowHeaderStyle> <Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource {x:Type DataGridRowHeader}}" > <!-- Empty content template for default state. Triggers below replace this for IsNewItem or IsEditing. --> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Label></Label> </DataTemplate> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding IsEditing, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True" > <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Label>Edit</Label> </DataTemplate> </Setter.Value> </Setter> </DataTrigger> <DataTrigger Binding="{Binding IsNewItem, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True" > <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Label>New</Label> </DataTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowHeaderStyle> </DataGrid>

更多推荐

WPF DataGrid自定义行标题

本文发布于:2023-11-08 07:45:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:义行   自定   标题   WPF   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!