簡體   English   中英

嵌套的INotifyPropertyChanged類不起作用

[英]Nested INotifyPropertyChanged class won't work

得到了一些代碼,正在得到意想不到的結果:

如果我將嵌套類替換為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;
    }
    }
} 

我猜你正在得到一個空引用錯誤,可能包裹在調用錯誤中,因為它很可能發生在構造函數中。

不要這樣做:

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. 確保所有類都實現了INotifyPropertyChanged。 特別是在處理UserControls時。
  2. 我不希望將屬性硬編碼為字符串-在這里查看我的文章如何使用反射來實現。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM