嵌套的INotifyPropertyChanged类不起作用

时间:2011-09-28 01:17:00

标签: c# wpf xaml data-binding nested

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

如果我用Myclass替换嵌套类,那么没有问题。我错过了什么? 如果我将文本绑定到另一个控件或绑定图像,则无关紧要。

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;
    }
    }
} 

2 个答案:

答案 0 :(得分:4)

我猜你得到一个空引用错误,可能包含在一个调用错误中,因为它可能发生在你的构造函数中。

不要这样做:

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

相反,使用空检查创建一个方法:

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

并称之为:

FirePropertyChange("MyClass");

答案 1 :(得分:0)

这里有几件事

  1. 确保所有类都实现了INotifyPropertyChanged。特别是在处理UserControls时。
  2. 我不想将属性硬编码为字符串 - 请查看我的帖子,了解如何使用反射执行此操作。
  3. http://tsells.wordpress.com/2011/02/08/using-reflection-with-wpf-and-the-inotifypropertychanged-interface/