c# - 嵌套 INotifyPropertyChanged 类不起作用

标签 c# wpf xaml data-binding nested

得到了一些代码,得到了意想不到的结果:

如果我用Myclass替换嵌套类,那就没有问题了。我想念什么? 我是否绑定(bind)文本(到其他控件)或绑定(bind)图像并不重要。

xaml代码:

<Window x:Class="WpfApplication1.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">
<Window.Resources>

    <DataTemplate x:Key="DataTemplate_Level">
        <Image  Source="{Binding Path=MyClass.ImageSource}" Width="48" Height="48"/>
    </DataTemplate>

</Window.Resources>
<Grid>
    <ItemsControl x:Name="h" ItemTemplate="{DynamicResource DataTemplate_Level}"/>
</Grid>
</Window>

类代码:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var myClass = new WrappedClass()
                          {
                              MyClass = new MyClass()
                          };

        var image = new BitmapImage(new Uri("Tiles.png", UriKind.Relative));
        int TileSize = 16;
        var cropRectangle = new Int32Rect((int)0, 0, TileSize, TileSize);
        var croppedBitmap = new CroppedBitmap(image, cropRectangle);


        var observableCollection = new ObservableCollection<WrappedClass>();
        observableCollection.Add(myClass);
        observableCollection.Add(myClass);
        observableCollection.Add(myClass);

        h.ItemsSource = observableCollection;
    }

    public class WrappedClass : INotifyPropertyChanged
    {
        private MyClass _myClass;
        public MyClass MyClass
        {
            get
            {
                return _myClass;
            }
            set
            {
                _myClass = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MyClass"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class MyClass : INotifyPropertyChanged
    {
        private ImageSource _imageSource;
        private string _text = "test";

        public MyClass()
        {
            var image = new BitmapImage(new Uri("Tiles.png", UriKind.Relative));
            int TileSize = 16;
            var cropRectangle = new Int32Rect((int)0, 0, TileSize, TileSize);
            _imageSource = new CroppedBitmap(image, cropRectangle);
        }

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
                PropertyChanged.Invoke(this,new PropertyChangedEventArgs("Text"));
            }
        }
        public ImageSource ImageSource
        {
            get
            {
                return _imageSource;
            }
            set
            {
                _imageSource = value;
                PropertyChanged.Invoke(this, new    PropertyChangedEventArgs("ImageSource"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    }
} 

最佳答案

我猜您遇到了空引用错误,可能包含在调用错误中,因为它很可能发生在您的构造函数中。

不要这样做:

PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MyClass"));

相反,创建一个带有 null 检查的方法:

    public void FirePropertyChange(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

并这样调用它:

FirePropertyChange("MyClass");

关于c# - 嵌套 INotifyPropertyChanged 类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7577332/

相关文章:

wpf - 我可以向样式添加资源或 ResourceDictionary 吗?

c# - WPF 将异常消息从 ThreadPool 线程传递到 UI

c# - Xamarin Forms - 如何创建可以输入数量的显示警报?

c# - 带有 TextBlock 的 BulletDecorator 不显示 Unicode 字符

c# - 需要为 nhibernate 配置提供程序集的路径

c# - SQL Server 2008 的墓碑问题

c# - 如何识别哪些方法是 getter 和 setter?

c# - 为什么两次比较只调用一次方法 Equals?

c# - 如何在WPF中向第二个实体添加CRUD操作

c# - 如何在绑定(bind)的 ItemsControl 中更改 ZIndex?