WPF数据网格更改所有列中的数据

本文关键字:数据 数据网 网格 WPF | 更新日期: 2023-09-27 18:02:14

在我的项目中,我有一个包含3个组合框模板列的数据网格,这些组合框是正确的数据绑定,当有不止一行时,如果我在任何一列中更改值,整个列中的数据更改为新值,如

只有一行


hello | hai | hai again

但是当添加新行时


this | hai | hai again

this | |

这里是mycode
<DataGrid x:Name="dtg"
              Grid.Row="2"
              AutoGenerateColumns="False"
              CanUserAddRows="True"
              IsReadOnly="False"
              SelectionUnit="CellOrRowHeader"
              ItemsSource="{Binding MainDataCollection, Mode= TwoWay}"
              AlternatingRowBackground="{DynamicResource AccentColorBrush2 }"
              GridLinesVisibility="Vertical"
              KeyUp="Dtg_OnKeyUp"
              Margin="10,10" >
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="slnoColunColumn"
                                Header="slno."
                                IsReadOnly="True"
                                Width="75"
                                Binding="{Binding Mode=OneWay , Path = Slno}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Category" Width="*" x:Name="categoryColumn">

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="categoryBox"
                            IsEditable="True"
                            controls:TextBoxHelper.ClearTextButton="True"
                            controls:TextBoxHelper.SelectAllOnFocus="True"
                            controls:TextBoxHelper.Watermark="Category"
                            MaxDropDownHeight="125"
                            SelectionChanged="CategoryBox_OnSelectionChanged"
                            IsSynchronizedWithCurrentItem="True"
                            DisplayMemberPath="CategoryName"
                            SelectedValuePath="CategoryId"   
                            SelectedItem="{Binding Category}"
                            ItemsSource="{Binding Path=DataContext.CategoriesCollection, 
                            RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Question" Width="*" x:Name="questionColumn">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="questionBox"
                            IsEditable="True"
                            controls:TextBoxHelper.ClearTextButton="True"
                            controls:TextBoxHelper.SelectAllOnFocus="True"
                            controls:TextBoxHelper.Watermark="Question"
                            MaxDropDownHeight="125"
                            IsSynchronizedWithCurrentItem="True"
                            DisplayMemberPath="TheQuestion"
                            SelectedValuePath="QuestionID"
                            SelectedItem="{Binding Question}"
                            ItemsSource="{Binding Path = DataContext.QuestionsCollection, 
                            RelativeSource = {RelativeSource FindAncestor, AncestorType = DataGrid}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Answer" Width="*" x:Name="AnswerColumn">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="answerswerBox"
                            IsEditable="True"
                            controls:TextBoxHelper.ClearTextButton="True"
                            controls:TextBoxHelper.SelectAllOnFocus="True"
                            controls:TextBoxHelper.Watermark="Question"
                            MaxDropDownHeight="125"
                            IsSynchronizedWithCurrentItem="True"
                            DisplayMemberPath="TheAnswer"
                            SelectedValuePath="AnswerID"
                            SelectedItem="{Binding Answer}"
                            ItemsSource="{Binding Path = DataContext.AnswersCollection, 
                            RelativeSource = {RelativeSource FindAncestor, AncestorType= DataGrid}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

有人知道为什么它是那样显示的吗?谁能建议一种方法来修复这个行为

WPF数据网格更改所有列中的数据

IsSynchronizedWithCurrentItem="True"出现问题的行删除行,问题解决