1

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:

access datagrid

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!

1
  • P.S.: i don't care about alternating row colors, i know this can done by setting AlternatingRowBackgroundin the DataGrid Commented Jan 25, 2017 at 15:46

1 Answer 1

2

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>            
Sign up to request clarification or add additional context in comments.

9 Comments

Thank your for your answer, but it doesn't seem to work. Maybe there is problem with binding to RowState. The Trigger Binding="{Binding RowState}" Value="EditRow"for example is never fired in my opinion!?
@CeOnSql I had to make assumptions because you didn't include much info about your viewmodels. Is the RowState property on the row items? Is it called RowState? Does it raise INotifyPropertyChanged.PropertyChanged when its value changes? If you share the source for the row item class itself, that will answer these questions.
I think you got me wrong RowStateis not a property of my underlying objects! With RowState i meant a property of the DataGrid (if this is existing!?)
@CeOnSql Thanks for the clarification. DataGridRow doesn't have a "state" property as such, but it does have some boolean flag properties that indicate new row state and editing state. I've updated the answer accordingly.
@CeOnSql Awesome!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.