简体   繁体   English

WPF DataGrid 未更新 - 是单向绑定吗?

[英]WPF DataGrid not updating - is it one-way binding?

C#, WPF. C#,WPF。 I am using a Datagrid with binding.我正在使用带有绑定的Datagrid My understanding is that with INotifyPropertyChanged implemented, object properties should update in the Datagrid if they are changed.我的理解是,在实现INotifyPropertyChanged ,如果对象属性发生更改,则它们应在Datagrid更新。

Currently this is not happening, although II have implemented INotifyPropertyChanged and I know from testing that the PropertyChanged event is firing.目前这并没有发生,尽管 II 已经实现了INotifyPropertyChanged并且我从测试中知道PropertyChanged事件正在触发。 My guess is that binding is not two-way(?) If that is the case I'm not sure how to set it to two-way.我的猜测是绑定不是双向的(?)如果是这种情况,我不确定如何将其设置为双向。 The binding is set in XAML , and the ItemsSource is set later in code-behind:绑定在XAML设置, ItemsSource稍后在代码隐藏中设置:

<DataGrid Name="dataGridxyz" ItemsSource="{Binding}">

dataGridxyz.ItemsSource = foo;

Adding two-way binding in XAML using this syntax causes an error:使用此语法在XAML添加双向绑定会导致错误:

<DataGrid Name="dataGridxyz" ItemsSource="{Binding, Mode=TwoWay}">

So I was looking for something like this:所以我一直在寻找这样的东西:

dataGridxyz.ItemsSource = foo;
dataGridxyz.Binding.Mode = TwoWay;

It may be that I could set it to two-way binding either in XAML or code-behind... but I can't see how to do either.可能我可以在XAML或代码隐藏中将其设置为双向绑定......但我看不出如何做。

EDIT:编辑:

The following is minimal functional example to show the problem.以下是显示问题的最小功能示例。 It is a much-simplified version of the real thing which is part of a much bigger project.它是真实事物的简化版本,是更大项目的一部分。

When the button is clicked, the Name property is changed but it does not update in the PropertyGrid .单击按钮时, Name属性会更改,但不会在PropertyGrid更新。

<Window x:Class="testBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow">
    <Grid>
        <StackPanel Orientation="Vertical">
            <DataGrid Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True"/>
            <Button Name="btn" Width="100" Height="20" Content="Test" Click="btn_Click"/>
        </StackPanel>
    </Grid>

namespace testBinding
{
    public partial class MainWindow : Window
    {
        BindingList<foo> bar = new BindingList<foo>() { new foo() };
        public MainWindow()
        {
            InitializeComponent();
            dg.ItemsSource = bar;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            bar[0].Name = "Paul";
        }
    }

    class foo : genericClass, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }

    class genericClass : INotifyPropertyChanged
    {
        private string _name = "John";
        public string EyeColor = "Blue";
        public bool Child = false;

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                MessageBox.Show("Name changed!"); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                OnPropertyChanged("Name");
            }
        }
    }
}

I figured out what was happening here through a combination of guesswork and trial and error.我通过猜测和反复试验找出了这里发生的事情。 Thanks to those who commented.感谢评论的人。

It was not caused by one-way binding as I had originally surmised.不是由我最初推测的单向绑定引起的。

This problem was caused by the fact that the foo object in the example above inherits from another class ( genericClass ) and both implement INotifyPropertyChanged .这个问题是由上例中的foo对象继承自另一个类 ( genericClass ) 并且都实现INotifyPropertyChanged It seems clear that the existence of the PropertyChanged event in the foo class prevents the DataGrid from updating.很明显, foo类中PropertyChanged事件的存在阻止了DataGrid更新。 I had not expected this behavior since I know that the PropertyChanged event in the inherited class does fire and does update the Name property.我没有预料到这种行为,因为我知道继承类中的PropertyChanged事件确实会触发并更新Name属性。

If I remove the PropertyChanged event from foo , then the name updates in the PropertyGrid as expected.如果我从foo删除PropertyChanged事件,则名称会按预期在 PropertyGrid 中更新。

class foo : genericClass, INotifyPropertyChanged
    {
        //public event PropertyChangedEventHandler PropertyChanged;
    }

It leaves me with the problem of how to handle property changes at more than one level of inheritance (ie both in a class and in one it inherits from, which seems a valid thing to do) ... but that is perhaps a different question.它给我留下了一个问题,即如何在一个以上的继承级别(即在一个类中和在它继承的一个类中处理属性更改,这似乎是一件有效的事情)……但这可能是一个不同的问题.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM