Creating editable GridviewColumn with DataTempülate

239 views Asked by At

I have an observable collection bound to my ListView via its ItemsSource property. I use a GridView to present the items in the ListView. By setting the "IsServiceMode" property to "true" in my ViewModel (DataContext to UserControl that houses the ListView) i want to change a GridViewColumn to use TextBox instead of Textblock. I'm using CellTemplates with ContentControl and DataTemplate as described here

However, the "Binding Position" in the Textblock and Textbox controls don't seem to work although the Editor is suggesting "Position". Position is a property of the items in the bound ObservableCollection and can be used via DisplayMemberBinding without Problems.

My GridViewColumn

<GridViewColumn Header="Position">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="{x:Type ContentControl}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding DataContext.IsServiceMode, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding Position, diag:PresentationTraceSources.TraceLevel=High}"/>
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding DataContext.IsServiceMode, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="True">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <TextBox Text="{Binding Position}"/>
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>´´´


1

There are 1 answers

0
i code On BEST ANSWER
  • The Datagrid comes with edit mode. IsReadOnly is turned on and off.
  • itemSource must be implemented INotifyCollectionChanged when editing mode is enabled.Like the ObservableCollection

Demo

        <DataGrid 
            x:Name="DataGrid1" 
            ItemsSource="{Binding Path=ObservableCollection}"
            IsReadOnly="{Binding Path=IsServiceMode}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Position" Binding="{Binding Path=Position,Mode=TwoWay}"/>
            </DataGrid.Columns>
        </DataGrid>