0

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!

CeOnSql
  • 2,615
  • 1
  • 16
  • 38
  • P.S.: i don't care about alternating row colors, i know this can done by setting `AlternatingRowBackground`in the DataGrid – CeOnSql Jan 25 '17 at 15:46

1 Answers1

1

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>            
  • 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 Jan 26 '17 at 07:50
  • @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. – 15ee8f99-57ff-4f92-890c-b56153 Jan 26 '17 at 14:14
  • I think you got me wrong `RowState`is not a property of my underlying objects! With `RowState` i meant a property of the DataGrid (if this is existing!?) – CeOnSql Jan 26 '17 at 14:51
  • @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. – 15ee8f99-57ff-4f92-890c-b56153 Jan 26 '17 at 15:06