C# 嵌套的INotifyPropertyChanged类赢得';行不通
获取了一些代码,但得到了意外的结果: 如果我用Myclass替换嵌套类,那么就没有问题了。我错过了什么? 无论是绑定文本(到其他控件)还是绑定图像 xaml代码:C# 嵌套的INotifyPropertyChanged类赢得';行不通,c#,wpf,xaml,data-binding,nested,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/20
<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;
}
}
}
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
var myClass=newwrappedclass()
{
MyClass=新的MyClass()
};
var image=新的位图图像(新的Uri(“Tiles.png”,UriKind.Relative));
int TileSize=16;
var cropRectangle=new Int32Rect((int)0,0,TileSize,TileSize);
var cropedbitmap=新的cropedbitmap(图像,cropedbitmap);
var observableCollection=新的observableCollection();
observableCollection.Add(myClass);
observableCollection.Add(myClass);
observableCollection.Add(myClass);
h、 ItemsSource=可观察到的收集;
}
公共类WrappedClass:INotifyPropertyChanged
{
私有MyClass MyClass;
公共MyClass MyClass
{
得到
{
返回我的类;
}
设置
{
_myClass=值;
调用(这是新的PropertyChangedEventArgs(“MyClass”);
}
}
公共事件属性更改事件处理程序属性更改;
}
公共类MyClass:INotifyPropertyChanged
{
私有ImageSource _ImageSource;
私有字符串_text=“test”;
公共MyClass()
{
var image=新的位图图像(新的Uri(“Tiles.png”,UriKind.Relative));
int TileSize=16;
var cropRectangle=new Int32Rect((int)0,0,TileSize,TileSize);
_imageSource=新裁剪的位图(图像,交叉);
}
公共字符串文本
{
得到
{
返回文本;
}
设置
{
_文本=值;
调用(这是新的PropertyChangedEventArgs(“文本”);
}
}
公共图像源图像源
{
得到
{
返回图像源;
}
设置
{
_imageSource=值;
调用(这是新的PropertyChangedEventArgs(“ImageSource”);
}
}
公共事件属性更改事件处理程序属性更改;
}
}
}
我猜您得到的是一个空引用错误,可能包含在调用错误中,因为它可能发生在您的构造函数中
不要这样做:
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。应用程序(不是示例)中是否有DynamicSource的wierd