嵌套的 INotifyPropertyChanged 类不起作用

Nested INotifyPropertyChanged class won#39;t work(嵌套的 INotifyPropertyChanged 类不起作用)
本文介绍了嵌套的 INotifyPropertyChanged 类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

got some code what is getting unexpected results:

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

If i replace the nested class with the Myclass, then there is no problem. What do I miss? It doesn't matter if I bind text (to an other control) or bind the image.

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

推荐答案

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

I'm guessing you are getting a null reference error, probably wrapped in an invocation error since it is likely happening in your constructor.

不要这样做:

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

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

Instead, create a method with a null check:

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

然后这样称呼它:

FirePropertyChange("MyClass");

这篇关于嵌套的 INotifyPropertyChanged 类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to do a full outer join in Linq?(如何在 Linq 中进行完整的外部联接?)
How do I group data in an ASP.NET MVC View?(如何在 ASP.NET MVC 视图中对数据进行分组?)
how to update the multiple rows at a time using linq to sql?(如何使用 linq to sql 一次更新多行?)
how to recognize similar words with difference in spelling(如何识别拼写不同的相似词)
C# Linq to SQL: How to express quot;CONVERT([...] AS INT)quot;?(C# Linq to SQL:如何表达“CONVERT([...] AS INT)?)
Why do quot;linq to sqlquot; queries starts with the FROM keyword unlike regular SQL queries?(为什么要“linq to sql与常规 SQL 查询不同,查询以 FROM 关键字开头?)