WPF数据网格组合框导致InvalidOperationException

人气:190 发布:2022-10-16 标签: wpf combobox datagrid invalidoperationexception

问题描述

我遇到InvalidOperationException(在AddNew或EditItem事务期间不允许使用‘DeferRefresh’。)当我尝试编辑组合框列的值时,从我的数据网格。我显示的所有项都引用了同一列表中的另一个项,所以这就是我使用组合框的目的。它被绑定到与数据网格相同的集合。我正在开发的应用程序以.NET3.5为目标,但我已经编写了一个在.NET4中完全相同的示例,因为数据网格是内置的。以下是数据网格中项目的代码:

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>

预先感谢您所能提供的任何帮助!

推荐答案

我发现了如何修复此问题。

我在我的Windows中创建了一个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}}"/>

929