WPF DataGrid组合框导致InvalidOperationException
当我试图编辑组合框列的值时,我从我的datagrid中获得一个InvalidOperationException(“在AddNew或EditItem事务期间不允许延迟刷新”)。我显示的项目都引用了同一列表中的另一个项目,所以这就是我使用组合框的目的。它与datagrid绑定到同一个集合。我正在开发的应用程序的目标是.NET3.5,但我已经编写了一个与.NET4完全相同的示例,因为datagrid是内置的。以下是datagrid中项目的代码:WPF DataGrid组合框导致InvalidOperationException,wpf,datagrid,combobox,invalidoperationexception,Wpf,Datagrid,Combobox,Invalidoperationexception,当我试图编辑组合框列的值时,我从我的datagrid中获得一个InvalidOperationException(“在AddNew或EditItem事务期间不允许延迟刷新”)。我显示的项目都引用了同一列表中的另一个项目,所以这就是我使用组合框的目的。它与datagrid绑定到同一个集合。我正在开发的应用程序的目标是.NET3.5,但我已经编写了一个与.NET4完全相同的示例,因为datagrid是内置的。以下是datagrid中项目的代码: public class TestItem : INo
public class TestItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int m_ID;
private string m_Name;
private int m_OppositeID;
public int ID
{
get { return m_ID; }
set
{
m_ID = value;
RaisePropertyChanged("ID");
}
}
public string Name
{
get { return m_Name; }
set
{
m_Name = value;
RaisePropertyChanged("Name");
}
}
public int OppositeID
{
get { return m_OppositeID; }
set
{
m_OppositeID = value;
RaisePropertyChanged("OppositeID");
}
}
public TestItem(int id, string name, int oppID)
{
ID = id;
Name = name;
OppositeID = oppID;
}
}
这是我的窗口中的代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<TestItem> m_Items;
public ObservableCollection<TestItem> Items
{
get { return m_Items; }
set
{
m_Items = value;
RaisePropertyChanged("Items");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Items = new ObservableCollection<TestItem>();
Items.Add(new TestItem(0, "Fixed", 0));
Items.Add(new TestItem(1, "Left Side", 2));
Items.Add(new TestItem(2, "Right Side", 1));
}
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
私人可观察收集m_项目;
公共可观测收集项目
{
获取{return m_Items;}
设置
{
m_项目=价值;
增加财产变更(“项目”);
}
}
公共主窗口()
{
初始化组件();
this.DataContext=this;
Items=新的ObservableCollection();
添加(新测试项(0,“固定”,0));
增加(新的测试项目(1,“左侧”,2));
增加(新的测试项目(2,“右侧”,1));
}
}
最后是我的xaml:
<Window x:Class="DataGrid_Combo_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False">
<DataGrid.Resources>
<Style x:Key="ItemsSourceStyle" TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Items, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" Width="*"/>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="*"/>
<DataGridComboBoxColumn Header="Opposite Item" Width="*" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=OppositeID}" ElementStyle="{StaticResource ItemsSourceStyle}" EditingElementStyle="{StaticResource ItemsSourceStyle}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
提前感谢您提供的任何帮助 我发现了如何解决这个问题 我在窗口中创建了一个CollectionViewSource,如下所示。参考资料:
<Window.Resources>
<CollectionViewSource x:Key="itemSource" Source="{Binding Path=Items}"/>
</Window.Resources>
然后将我的组合框列定义更改为以下内容:
<DataGridComboBoxColumn Header="Opposite Item" Width="*" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=OppositeID}" ItemsSource="{Binding Source={StaticResource itemSource}}"/>
在调用
CollectionView
或DataGridXYZ.Items
DataGridX.CommitEdit();
DataGridX.CancelEdit();
为我工作。这似乎与Connect()和MSDN论坛()上报告的问题有关;但这是迄今为止最优雅的解决方法!我遇到这个问题是因为我错误地将我的DataGrid和DataGridComboxColumn绑定到同一个集合。发布的代码只是这个问题的一个简短演示。真正的应用程序使用mvvm,这种方法意味着附加事件处理程序等。