WPF DataGrid ComboBox 导致 InvalidOperationException

6

当我尝试编辑组合框列的值时,我的数据表格会出现InvalidOperationException('DeferRefresh' is not allowed during an AddNew or EditItem transaction.)异常。我显示的所有项目都与同一列表中的另一个项目有关联,因此这就是我使用组合框的原因。它绑定到与数据表格相同的集合。我正在开发的应用程序针对.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>

感谢您提前提供的任何帮助!

1
这似乎与Connect(https://connect.microsoft.com/VisualStudio/feedback/details/591125/datagrid-exception-on-validation-failure-deferrefresh-is-not-allowed)和MSDN论坛(http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/187b2b8f-d403-4bf3-97ad-7f93b4385cdf/)上报告的问题有关; 但这是迄今为止最优雅的解决方法! - David Schmitt
我遇到了这个问题,是因为我错误地将我的DataGrid和DataGridComboBoxColumn绑定到了同一个集合上。 - Maxence
2个回答

3

我找到了解决这个问题的方法。

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

2
在调用 CollectionViewDataGridXYZ.ItemsRefersh 前,请尝试按照以下顺序操作:
DataGridX.CommitEdit();

DataGridX.CancelEdit();

对我有用。


发布的代码只是问题的简短演示。真正的应用程序使用mvvm,这种方法意味着需要附加事件处理程序等。 - aalex675

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接