WPF DataGrid自定义行标题 [英] WPF DataGrid Custom Row Header

查看:215
本文介绍了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自定义行标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆