C# 在WPF xaml文件中绑定ObservalEditionary
我有一个名为C# 在WPF xaml文件中绑定ObservalEditionary,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个名为myCollection的ObservableCollection对象,我使用.XAML文件中的在GUI中显示该对象(请参见下面的代码)。 但是现在,myCollection的类型已更改为字典。 现在我该如何呈现它?我发现了一个observedictionary的实现。但我不知道如何将它绑定到我的GUI? 谢谢你的帮助 <Page x:Class="Project.ThisPage" xmlns="http://schemas.microsoft.com/winfx/200
myCollection
的ObservableCollection
对象,我使用.XAML
文件中的
在GUI中显示该对象(请参见下面的代码)。
但是现在,myCollection
的类型已更改为字典
。
现在我该如何呈现它?我发现了一个observedictionary
的实现。但我不知道如何将它绑定到我的GUI?
谢谢你的帮助
<Page x:Class="Project.ThisPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-ThisPage.ProjectSpace"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="640"
Title="ThisPage"
Name="thisPage">
<Page.Resources>
<DataTemplate x:Key="MyDataTemplate" DataType="{x:Type local:MyDataType}">
<Label Content="{Binding Name}"/>
<Label Content="{Binding Id}"/>
</DataTemplate>
</Page.Resources>
<Grid>
<ScrollViewer>
<StackPanel>
<ItemsControl ItemsSource="{Binding ElementName=thisPage,Path=myCollection}" ItemTemplate="{StaticResource MyDataTemplate}" />
</StackPanel>
</ScrollViewer>
</Grid></Page>
myCollection.Values
将为您提供MyDataType集合,myCollection.Keys
将为您提供UINT64集合。因此绑定到MyDataType集合只需更新绑定,如下所示:
ItemsControl ItemsSource="{Binding ElementName=thisPage,Path=myCollection.Values}" ItemTemplate="{StaticResource MyDataTemplate}" />
我尝试了,但最终出现了一个错误
System.Windows.Data错误:40:BindingExpression路径错误:“在对象”“ThisPage'(Name='ThisPage')上找不到myCollection”属性。BindingExpression:Path=myCollection.Values;DataItem='ThisPage'(Name='ThisPage');目标元素是“ItemsControl”(名称=“”);目标属性为“ItemsSource”(类型为“IEnumerable”)
确保页面的DataContext已设置属性,然后确保ItemsControl ItemsSource=“{Binding ElementName=thisPage,Path=DataContext.myCollection.Values}”ItemTemplate=“{StaticResource MyDataTemplate}”/>。这很有趣,因为当您尝试使用Collection而不是dictionary时,它也会失败。如何正确设置DataContext?您在哪里有myCollection属性?在这个页面的代码隐藏中?