gpt4 book ai didi

c# - 嵌套的 INotifyPropertyChanged 类将不起作用

转载 作者:行者123 更新时间:2023-11-30 21:12:18 25 4
gpt4 key购买 nike

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

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

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

关于c# - 嵌套的 INotifyPropertyChanged 类将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7577332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com