【问题标题】:selection change in wpf template combo box column changes values in other rowswpf模板组合框列中的选择更改更改其他行中的值
【发布时间】:2015-09-30 09:55:46
【问题描述】:

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

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

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

添加数据的方法

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()));
        }

    }

添加新数据网格行的方法

 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);            
    }

【问题讨论】:

    标签: c# wpf wpfdatagrid datagridtemplatecolumn


    【解决方案1】:

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

    IsSynchronizedWithCurrentItem="False"

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

    【讨论】:

    • 所有这一切最终绑定到的代码。
    • 好的,我发现了一个问题。每当您选择一个新值时,您都会清除最终绑定的对象集合。由于这些集合用于所有行,因此清除集合中的所有内容将干扰使用同一集合的所有其他组合框中的值。您将需要使用不同的方法,以便每个组合框都使用自己的集合,然后可以在需要时对其进行修改(可能作为集合中项目的属性公开)。
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多