WPF DataGrid ComboBox 导致 InvalidOperationException
当我尝试编辑组合框列的值时,我从数据网格中收到 InvalidOperationException(在 AddNew 或 EditItem 事务期间不允许“DeferRefresh”。)。我显示的所有项目都引用了同一列表中的另一个项目,因此这就是我使用组合框的目的。它绑定到与数据网格相同的集合。我正在开发的应用程序面向 .NET 3.5,但我已经整理了一个与 .NET 4 中完全相同的示例,因为数据网格是内置的。这是数据网格中项目的代码:
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));
}
}
最后是我的 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>
提前感谢您提供的任何帮助!
I am getting an InvalidOperationException('DeferRefresh' is not allowed during an AddNew or EditItem transaction.) from my datagrid when I try to edit the value of a combo box column. The items I am showing all have a reference to one other item in the same list so this is what I am using the combobox for. It is bound to the same collection as the datagrid is. My application I am working on is targetting .NET 3.5, but I have put together an example that is exactly the same in .NET 4 since the datagrid is built in. Here is the code for items in the datagrid:
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;
}
}
This is the code in my window:
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));
}
}
and finally my 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>
Thanks in advance for any assistance you can offer!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了如何解决这个问题。
我在 Window.Resources 中创建了这样的 CollectionViewSource:
然后将组合框列定义更改为以下内容:
I found out how to fix this issue.
I created a CollectionViewSource like this in my Window.Resources:
Then changed my combobox column definition to the following:
在调用
CollectionView
或DataGridXYZ.Items
上的Refersh
之前尝试以下顺序对我有用。
Try following sequence before calling
Refersh
onCollectionView
orDataGridXYZ.Items
Worked for me.