C# wpf模板组合框列中的选择更改会更改其他行中的值
在我的项目中,我有一个带有三个组合框模板列的数据网格,数据绑定到三个集合,这些列是可编辑的。第二个和第三个组合框中的集合是根据第一列中组合框的选定索引选择的。当我更改任何行中第一列中的选择时,更改将反映在所有行中。这是我的xaml代码C# wpf模板组合框列中的选择更改会更改其他行中的值,c#,wpf,wpfdatagrid,datagridtemplatecolumn,C#,Wpf,Wpfdatagrid,Datagridtemplatecolumn,在我的项目中,我有一个带有三个组合框模板列的数据网格,数据绑定到三个集合,这些列是可编辑的。第二个和第三个组合框中的集合是根据第一列中组合框的选定索引选择的。当我更改任何行中第一列中的选择时,更改将反映在所有行中。这是我的xaml代码 <DataGrid x:Name="dtg" Grid.Row="2" AutoGenerateColumns="False" CanUserAddRows="True"
<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 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);
}
尝试将其添加到所有3个组合框:
IsSynchronizedWithCurrentItem=“False”
我猜这个问题以前已经在这个网站上解决了:)编码到所有这些最终都绑定到的东西。好的,我看到了一个问题。您正在清除最终在选择新值时绑定到的对象集合。由于这些集合跨所有行使用,因此清除集合中的所有内容将干扰使用同一集合的所有其他组合框中的值。您需要使用不同的方法,以便组合框每个都使用自己的集合,然后可以在需要时对其进行修改(可能作为集合中项目的属性公开)。
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);
}