[英]Bind an element of Array to a xaml property
Before I get a duplicate question, I just want to say I can't seem to find a solution to my problem at all.在收到重复的问题之前,我只想说我似乎根本找不到解决问题的方法。 All I want to do is bind an element of an array to a property and can't seem to figure out how because of my lack of experience.我想要做的就是将数组的一个元素绑定到一个属性,但由于我缺乏经验,似乎无法弄清楚如何。
Here is how my C# code looks:这是我的 C# 代码的外观:
public class MainPageImplement : INotifyPropertyChanged { BitmapImage[] weatherIconSource = new BitmapImage[7]; public BitmapImage[] WeatherIconSource { get { return weatherIconSource; } set { weatherIconSource = value; NotifyPropertyChanged("WeatherIconSource"); } } private void NotifyPropertyChanged(string propName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); } public event PropertyChangedEventHandler PropertyChanged; }
Here is how my XAML property looks:这是我的 XAML 属性的外观:
<Image Grid.Column="1" Name="WeatherIconZero" Source="{x:Bind PropertyName.WeatherIconSource[0], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" />
My code executes without any errors to go off of but does not actually bind anything.我的代码执行时没有任何错误,但实际上并没有绑定任何东西。 However, it works just fine if I used BitmapImage variable and not an element from array.但是,如果我使用 BitmapImage 变量而不是数组中的元素,它就可以正常工作。
Arrays are not bindable to the UI, at least not per index.数组不能绑定到 UI,至少不能绑定到每个索引。 You need to use an ObservableCollection<T>
:您需要使用ObservableCollection<T>
:
public ObservableCollection<BitmapImage> WeatherIconSource { get; } = new ObservableCollection<BitmapImage>();
Don't bother using arrays;不要费心使用数组; you cannot notify a single-index property change您无法通知单索引属性更改
NotifyPropertyChanged("WeatherIconSource[5]"); // Doesn't work.
If you're stuck with arrays, then you have to notify property changed on the entire collection whenever you update a single index:如果您坚持使用数组,那么每当您更新单个索引时,您都必须通知整个集合的属性已更改:
WeatherIconSource[5] = myNewBitmap;
NotifyPropertyChange("WeathIconSource"); // Updates all [0] [1] ... [n]
But if you do this, you will force a refresh on all indexes.但是如果你这样做,你将强制刷新所有索引。 All your images will redraw.您的所有图像都将重绘。 You may get away with this if your app is simple with small images;如果您的应用程序很简单,带有小图片,您可能会逃脱; otherwise, make the change to ObservableCollection<T>
.否则,更改ObservableCollection<T>
。
If you have hardcoded bindings with an exact index, like this:如果您有具有精确索引的硬编码绑定,如下所示:
Source="{x:Bind PropertyName.WeatherIconSource[0], Mode=OneWay}"
Source="{x:Bind PropertyName.WeatherIconSource[2], Mode=OneWay}"
Source="{x:Bind PropertyName.WeatherIconSource[5], Mode=OneWay}"
then instantiate your collection like this so you don't get an IndexOutOfRangeException
:然后像这样实例化你的集合,这样你就不会得到IndexOutOfRangeException
:
public ObservableCollection<BitmapImage> WeatherIconSource { get; } = new ObservableCollection<BitmapImage>
{
null, // [0]
null, // [1]
null,
null,
null,
null,
null // [6]
};
它应该是,
`<Image Grid.Column="1" Name="WeatherIconZero" Source="{Binding weatherIconSource[0]}" HorizontalAlignment="Center" VerticalAlignment="Center" /`>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.