Nested INotifyPropertyChanged class won#39;t work(嵌套的 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 类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:嵌套的 INotifyPropertyChanged 类不起作用
基础教程推荐
- 当键值未知时反序列化 JSON 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 创建属性设置器委托 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01