在我得到重复问题之前,我只想说我似乎无法找到解决我的问题的方法。我想要做的就是将一个数组的元素绑定到一个属性,并且似乎无法弄清楚我是如何因为缺乏经验。将Array的元素绑定到xaml属性
这里是我的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; }
下面是我的XAML属性如下:
<Image Grid.Column="1" Name="WeatherIconZero" Source="{x:Bind PropertyName.WeatherIconSource[0], Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" />
我的代码的执行没有任何错误去关闭但实际上并没有绑定任何东西。但是,如果我使用BitmapImage变量而不是数组中的元素,它工作得很好。
你是如何设置WeatherIconSource属性?因为如果你单独修改每个索引(即'WeatherIconSource [0] = myBitmap;'),那么你将不会通知UI的属性更改。如果您只是简单地使用'ObservableCollection',您将节省很多痛苦。数组不会将更改传达给UI。 –
Laith
我可以说我有一系列能够获得正确财产的方法吗?其他一些属性也会共享这些方法中的一部分。为了更新UI,我设置了一个计时器来调用更新这些数组的方法。每当它更新数组时,都会有一个调用PropertyChanged来更新UI的'set'。当我不使用数组并为每个属性提供一个唯一的路径时,此功能完美无瑕。但是,如果我使用数组,它会使我的代码看起来更好,并节省数百行代码。编辑:我应该添加我在不同的线程上运行的一天中的某些时间更新UI。 – Oybek
好吧,我的回答如下:) – Laith