简体   繁体   English

在WPF xaml文件中绑定ObservableDictionary

[英]Bind an ObservableDictionary in WPF xaml file

I had an ObservableCollection<MyDataType> object named myCollection that I was presenting in a GUI using a <DataTemplate> in my .XAML file (See code below). 我有一个ObservableCollection<MyDataType>对象命名myCollection ,我是在GUI使用呈现<DataTemplate>在我.XAML文件(参见下面的代码)。 But now the type of myCollection has changed to a Dictionary<UInt64,MyDataType> . 但是现在myCollection的类型已更改为Dictionary<UInt64,MyDataType> How would I present it now? 我现在将如何呈现? Here I found an implementation of an ObservableDictionary<> . 在这里,我找到了ObservableDictionary<> But I don't know how I would bind this to my GUI? 但是我不知道如何将其绑定到我的GUI? Thank you for any assistance! 感谢您的协助!

<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 will give you MyDataType collection and myCollection.Keys will give you UINT64 collection. myCollection.Values将给您MyDataType集合,而myCollection.Keys将给您UINT64集合。 So bind to MyDataType collection just update binding like: 因此,绑定到MyDataType集合只需更新绑定,例如:

ItemsControl ItemsSource="{Binding ElementName=thisPage,Path=myCollection.Values}" ItemTemplate="{StaticResource MyDataTemplate}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM