WPF数据网格更改所有列

时间:2015-07-10 21:41:51

标签: c# wpf wpfdatagrid

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

只有一行

你好|海|海再次

但是当添加新行时

这个|海|海再次

这个| |

这是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="answerBox"
                            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>

任何人都知道为什么会这样表现出来。任何人都可以建议一种方法来解决这种行为

1 个答案:

答案 0 :(得分:0)

IsSynchronizedWithCurrentItem="True"正在解决问题并解决了问题