WPF模板组合框列中的选择更改会更改其他行中的值

WPF模板组合框列中的选择更改会更改其他行中的值

本文介绍了WPF模板组合框列中的选择更改会更改其他行中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个包含三个组合框模板列的数据网格,并且数据绑定到三个集合,并且这些列是可编辑的。当我在任何行的第一列中更改选择时,更改将反映在所有行中,因此根据第一列中组合框的选定索引来选择第二个和第三个组合框中的集合。这是我的xaml代码

In my project I have a data grid with three combo box template columns and are data bound to three collections and these columns are editable. The collection in the second and third combo box is selected based on the selected index of the combo box in first column when I change selection in first column in any row the change is reflected in all rows. here is my xaml code

<DataGrid x:Name="dtg"
              Grid.Row="2"
              AutoGenerateColumns="False"
              CanUserAddRows="True"
              IsReadOnly="False"
              VerticalScrollBarVisibility="Visible"
              EnableRowVirtualization="False"
              SelectionUnit="CellOrRowHeader"
              SelectionMode="Extended"
              ItemsSource="{Binding MainDataCollection, Mode= TwoWay}"
              AlternatingRowBackground="{DynamicResource AccentColorBrush2 }"
              GridLinesVisibility="Horizontal"
              KeyUp="Dtg_OnKeyUp" >
        <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"
                            DisplayMemberPath="CategoryName"
                            SelectedValuePath="CategoryId"
                            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"
                            DisplayMemberPath="TheQuestion"
                            SelectedValuePath="QuestionID"
                            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="Answer"
                            MaxDropDownHeight="125"
                            DisplayMemberPath="TheAnswer"
                            SelectedValuePath="AnswerID"
                            ItemsSource="{Binding Path = DataContext.AnswersCollection,
                            RelativeSource = {RelativeSource FindAncestor, AncestorType= DataGrid}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

任何人都知道为什么会这样。谁能找到解决此问题的解决方案

anybody know why it is behaving like this. Can anyone find a solution to fix this

添加数据的方法

public void AddData()
    {
        if (MainDataCollection== null) MainDataCollection = new ObservableCollection<GridDataSource>();
        if (CategoriesCollection == null) CategoriesCollection = new ObservableCollection<Category>();
        if(QuestionsCollection == null) QuestionsCollection = new ObservableCollection<Question>();
        if(AnswersCollection == null) AnswersCollection = new ObservableCollection<Answer>();

        AddDataGridRow(0);
        var data = _dataProvider.GetCategoriesTable(0);

        foreach (DataRow row in data.Rows)
        {

                CategoriesCollection.Add(new Category(int.Parse(row[0].ToString()),row[1].ToString()));
        }

    }

添加新数据网格行的方法

method to add new data grid row

 public void AddDataGridRow(int slno)
    {
        MainDataCollection.Add(new GridDataSource(slno+1,"","",""));
    }

获取问题的方法

public void GetQuestions(int categoryId)
    {
        if (QuestionsCollection == null)QuestionsCollection = new ObservableCollection<Question>();
        QuestionsCollection.Clear();
        var data = _dataProvider.GetQuestionsTable(categoryId);
        foreach (DataRow row in data.Rows)
        {
            QuestionsCollection.Add(new Question(int.Parse(row[0].ToString()),row[1].ToString()));
        }

        GetAnswers(categoryId);
    }

获取答案的方法

 public void GetAnswers(int categoryId)
    {
        if (AnswersCollection == null) AnswersCollection = new ObservableCollection<Answer>();
        AnswersCollection.Clear();
        var data = _dataProvider.GetAnswersTable(categoryId);
        foreach (DataRow row in data.Rows)
        {
            AnswersCollection.Add(new Answer(int.Parse(row[0].ToString()), row[1].ToString()));
        }

    }

组合框选择已更改

private void CategoryBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox cb = sender as ComboBox;
        _mainWindowViewModel.GetQuestions((int)cb.SelectedValue);
    }


推荐答案

尝试将其添加到所有3个组合框:

Try adding this to all 3 comboboxes:

IsSynchronizedWithCurrentItem = False

我猜这之前已经在此网站上解决了:)

I'm guessing this has been solved on this site before :)

这篇关于WPF模板组合框列中的选择更改会更改其他行中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:37