【问题标题】:Nested INotifyPropertyChanged class won't work嵌套的 INotifyPropertyChanged 类不起作用
【发布时间】:2011-11-26 12:00:01
【问题描述】:

得到了一些代码是什么得到了意想不到的结果:

如果我用 Myclass 替换嵌套类,则没有问题。我想念什么? 绑定文本(到其他控件)或绑定图像都没有关系。

xml代码:

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

【问题讨论】:

    标签: c# wpf xaml data-binding nested


    【解决方案1】:

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

    不要这样做:

    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");
    

    【讨论】:

    • 没有捕获处理程序 == null。在应用程序(不是示例)中,DynamicResource 是否也有一些奇怪的地方
    【解决方案2】:

    这里有几件事

    1. 确保所有类都实现了 INotifyPropertyChanged。尤其是在处理 UserControl 时。
    2. 我不希望将属性硬编码为字符串 - 请查看我的帖子,了解如何使用反射来执行此操作。

    http://tsells.wordpress.com/2011/02/08/using-reflection-with-wpf-and-the-inotifypropertychanged-interface/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2018-03-19
      • 1970-01-01
      • 2023-03-31
      • 2014-12-20
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多