大佬教程收集整理的这篇文章主要介绍了ObservabaleCollection 添加/删除不会更新 xaml 端的 ItemsControl,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一个数据类:
class MyData
{
public String Firstname {get;set;}
public String Lastname {get;set;}
}
然后在我的视图模型中我有
class Myviewmodel : ModelBase // assume this has all the work behind for prop notification changes
{
private ObservableCollection<MyData> addedData = new ObservableCollection<MyData>();
public ObservableCollection<MyData> AddedData
{
get
{
return this.addedData;
}
set
{
this.addedData = value;
this.RaisePropertyChangedEvent();
}
}
public voID AddDatarequestHandler()
{
this.AddedData.Add(new MyData() {some firstname and lastnamE}); //prototyPing
}
}
在 xaml 方面,我有这样的东西
<GrID
<GrID.RowDeFinitions>
<RowDeFinition Height="auto" />
<RowDeFinition Height="auto" />
</GrID.RowDeFinitions>
<button GrID.Row="0" Content="Add" Command="{Binding Path=AddDataCommanD}" />
<ItemsControl GrID.Row="1" Itemssource="{Binding Path=AddedData}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl Itemssource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel OrIEntation="Horizontal">
<TextBlock Text="{Binding Path=FirstnamE}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding Path=LastnamE}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GrID>
并假设我的添加按钮命令请求处理程序正确绑定到视图模型中的处理程序(我可以通过它进行调试)。
但是,当我单击“添加”按钮并且我可以调试并看到可观察集合属性 AddData 将 MyData 类型的项目添加到其中时,ItemsControl 仍然没有显示我添加到堆栈面板的任何项目。我在这里做错了什么?
我的错,我没有注意到我在 xaml 方面有过度的嵌套。所以 ItemsControl 应该简单得多,如下所示,它可以正常工作:
<Grid
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Add" Command="{Binding Path=AddDataCommanD}" />
<ItemsControl Grid.Row="1" Itemssource="{Binding Path=AddedData}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstNamE}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding Path=LastNamE}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
以上是大佬教程为你收集整理的ObservabaleCollection 添加/删除不会更新 xaml 端的 ItemsControl全部内容,希望文章能够帮你解决ObservabaleCollection 添加/删除不会更新 xaml 端的 ItemsControl所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。