Search code examples
c#wpfwpfdatagrid

WPF data grid changing data in all columns


in my project i have a data grid which contains 3 combo box template columns and those combo boxes are properly data bound, when there is more than one row and if i change value in any of the column, the data in the entire column changes to new value like this

only one row


hello | hai | hai again

but when a new row is added


this | hai | hai again

this | |

here is 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>

Anybody know why it it is showing like that. Can anyone suggest a way to fix this behaviour


Solution

  • the line IsSynchronizedWithCurrentItem="True" was making problems removed the line and problem solved