字符串[]和List<不同的绑定行为;串> [英] Different binding behavior for string[] and List<string>
问题描述
我试着去理解(没有成功),为什么绑定差异的行为时源对象是的String []
和列表<串>
。我有两个名单,他们唯一的区别是的的ItemsSource 的 - 在一个案例中的数组中的第二个列表的的:
XAML代码:
<预类=郎咸平的XML prettyprint-覆盖>
< StackPanel的背景={ThemeResource ApplicationPageBackgroundThemeBrush} >
<按钮内容=修改项目点击=Button_Click/>
< StackPanel的方向=横向>
< StackPanel.Resources>
<的DataTemplate X:键=ItemTemplate中>
< TextBlock的文本={结合}字号=16/>
< / DataTemplate中>
< /StackPanel.Resources>
< ListView控件的ItemsSource ={结合ArrayElements}的ItemTemplate ={StaticResource的ItemTemplate中}WIDTH =100保证金=20/>
< ListView控件的ItemsSource ={结合ListElements}的ItemTemplate ={StaticResource的ItemTemplate中}WIDTH =100保证金=20/>
< / StackPanel的>
< / StackPanel的>
后面的代码:
<预类=郎-cs prettyprint-覆盖>
公共密封部分类的MainPage:页面,INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效RaiseProperty(字符串名称)=> ?的PropertyChanged .Invoke(这一点,新PropertyChangedEventArgs(名));
公共字符串[] {ArrayElements获得; } =新的字符串[] {标准,标准,标准};
公开名单<串GT; ListElements {搞定; } =新的List<串GT; {标准,标准,标准};
公众的MainPage(){this.InitializeComponent(); this.DataContext =这一点; }
私人无效Button_Click(对象发件人,RoutedEventArgs E)
{
ArrayElements [1] =修改一;
ListElements [1] =修改一;
RaiseProperty(nameof(ArrayElements));
RaiseProperty(nameof(ListElements));
}
}
在我点击按钮,第一个列表(建立在数组)获取与新元素重新填充(看起来像新的的ItemsSource 的),这样我就可以看到一个小的闪烁,第二个元素的变化。
在第二个列表中没有同时变化 - 参考绑定源并没有改变,所以我们看不到屏幕上的差异(的PropertyChange 的有,因为没有财产已被更改无效) - 是十分明显的。
那么,为什么其中的的ItemsSource 的设置为数组列表表现有什么不同?
的样品来重现问题(尽管几乎整个代码是以上)
这听起来怪我,但因为我观察到一个集合中使用ElementViewModel对象,我从来没有遇到这个问题。相反,对列表项/数组元素的i更改ElementViewModel的属性,并使用INotifyProperty改变那里。你的问题,但可以解决这个它不是实际工作的答案。
I try to understand (without success) why binding behaves differentially when source object is string[]
and List<string>
. I have two lists, their only difference is ItemsSource - in one case array in second List:
XAML code:
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Modify items" Click="Button_Click"/>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding}" FontSize="16"/>
</DataTemplate>
</StackPanel.Resources>
<ListView ItemsSource="{Binding ArrayElements}" ItemTemplate="{StaticResource ItemTemplate}" Width="100" Margin="20"/>
<ListView ItemsSource="{Binding ListElements}" ItemTemplate="{StaticResource ItemTemplate}" Width="100" Margin="20"/>
</StackPanel>
</StackPanel>
Code behind:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public string[] ArrayElements { get; } = new string[] { "Standard", "Standard", "Standard" };
public List<string> ListElements { get; } = new List<string> { "Standard", "Standard", "Standard" };
public MainPage() { this.InitializeComponent(); this.DataContext = this; }
private void Button_Click(object sender, RoutedEventArgs e)
{
ArrayElements[1] = "Modified one";
ListElements[1] = "Modified one";
RaiseProperty(nameof(ArrayElements));
RaiseProperty(nameof(ListElements));
}
}
Once I click button, the first list (build upon array) gets refilled with new elements (seems like new ItemsSource), so I can see a little flicker and the change in second element.
The same time in the second list nothing changes - the reference to binding source doesn't change so we don't see the difference on screen (PropertyChange has no effect as no property has been changed) - that's clear.
So why the list where the ItemsSource is set to array behaves different?
Sample to reproduce the issue (though almost whole code is above)
It sounds weird to me but i never faced this Problem because i use ElementViewModel objects in an observable collection. Instead of changing the list-item / Array element i Change a property of the ElementViewModel and use the INotifyProperty Change there. It's not the actuall answer to you question but you can work around with this.
这篇关于字符串[]和List<不同的绑定行为;串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!