C# 使用字典索引器进行数据绑定

C# 使用字典索引器进行数据绑定,c#,dictionary,mvvm,data-binding,win-universal-app,C#,Dictionary,Mvvm,Data Binding,Win Universal App,我是UWP和MVVM的新手 我在数据绑定方面遇到了一些问题 我有一处房产 public System.DayOfWeek CurrentDayOfWeek 还有一本字典 public MyDictionary Dictionary<System.DayOfWeek,List<String>> 公共MyDictionary 我想将ListView的ItemSource绑定到MyDictionary[CurrentDayOfWeek] 以下是我所奋斗的: 视图模型:

我是UWP和MVVM的新手

我在数据绑定方面遇到了一些问题

我有一处房产

public System.DayOfWeek CurrentDayOfWeek 
还有一本字典

public MyDictionary Dictionary<System.DayOfWeek,List<String>>
公共MyDictionary
我想将ListView的ItemSource绑定到MyDictionary[CurrentDayOfWeek]

以下是我所奋斗的:

视图模型:

public Dictionary<System.DayOfWeek,List<string>> MyDictionary
{
    get;set;
}
public System.DayOfWeek CurrentDayOfWeek {get;set;}
public List<System.DayOfWeek> DayOfWeeks {get;set;}
.....        
公共字典MyDictionary { 获得;设置; } public System.DayOfWeek CurrentDayOfWeek{get;set;} 公共列表DayOfWeeks{get;set;} ..... XAML:

您可以尝试在combobox中显示字典的键,然后,Binding ElementName将在combobox中为您提供所选项以定义ListView绑定

                <ComboBox x:Name="DropDown"
                   ItemsSource="{Binding ViewModel.CurrentDayOfWeek}"
                   DisplayMemberPath ="Key"
                   SelectedValuePath ="Value"
                   IsReadOnly="True"></ComboBox>

                <ListView x:Name="List"
                   ItemsSource="{Binding ElementName = DropDown, Path = SelectedValue}"
                   DisplayMemberPath ="Value"
                   SelectedValuePath ="Value"/>

一个“优雅的解决方案”是非常固执己见的,对我来说,最优雅的解决方案就是有效的解决方案!词典很难装订。不要使用它们。如果您需要在绑定中建立索引(如果您认为必须重构,则不应该重构),请创建一个扩展KeyedCollection的集合。您可以灵活地实现inotifycollection,将其更改为具有可绑定的索引集合。
                <ComboBox x:Name="DropDown"
                   ItemsSource="{Binding ViewModel.CurrentDayOfWeek}"
                   DisplayMemberPath ="Key"
                   SelectedValuePath ="Value"
                   IsReadOnly="True"></ComboBox>

                <ListView x:Name="List"
                   ItemsSource="{Binding ElementName = DropDown, Path = SelectedValue}"
                   DisplayMemberPath ="Value"
                   SelectedValuePath ="Value"/>