简体   繁体   中英

WPF DataGrid Custom Row Header

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

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

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

访问数据网格

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

  • RowState.Editing: Show Edit Icon
  • RowState.NewRow: Show Star
  • RowState.Default: Show Default Row Header

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

Thank you!

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 .

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>            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM