WPF DataGrid ComboBox 导致 InvalidOperationException

发布于 2024-09-14 02:57:42 字数 3623 浏览 6 评论 0原文

当我尝试编辑组合框列的值时,我从数据网格中收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-09-21 02:57:42

我找到了如何解决这个问题。

我在 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}}"/>

I found out how to fix this issue.

I created a CollectionViewSource like this in my Window.Resources:

<Window.Resources>
    <CollectionViewSource x:Key="itemSource" Source="{Binding Path=Items}"/>
</Window.Resources>

Then changed my combobox column definition to the following:

<DataGridComboBoxColumn Header="Opposite Item" Width="*" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=OppositeID}" ItemsSource="{Binding Source={StaticResource itemSource}}"/>
北凤男飞 2024-09-21 02:57:42

在调用 CollectionViewDataGridXYZ.Items 上的 Refersh 之前尝试以下顺序

DataGridX.CommitEdit();

DataGridX.CancelEdit();

对我有用。

Try following sequence before calling Refersh on CollectionView or DataGridXYZ.Items

DataGridX.CommitEdit();

DataGridX.CancelEdit();

Worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文