Wpf 按键绑定字典项

Wpf 按键绑定字典项,wpf,dictionary,binding,Wpf,Dictionary,Binding,假设我有一些字典,我想将这个字典中的项绑定到一些控件,我想按项键绑定 public partial class Window1 : Window { public Window1() { InitializeComponent(); Dictionary<string,string> dictionary = new Dictionary<string,bool>(); dictionary.Add("I

假设我有一些字典,我想将这个字典中的项绑定到一些控件,我想按项键绑定

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        Dictionary<string,string> dictionary = new Dictionary<string,bool>();

        dictionary.Add("Item 1", "one");
        dictionary.Add("Item 2", "two");
        dictionary.Add("Item 3", "three");
        // ...
        dictionary.Add("Item 99", "ninety nine");

        // ...

        this.DataContext = //...
    }
}
公共部分类窗口1:窗口
{
公共窗口1()
{
初始化组件();
字典=新字典();
字典。添加(“第1项”、“一项”);
添加(“第2项”、“第2项”);
添加(“第3项”、“第3项”);
// ...
字典。添加(“第99项”、“第九十九项”);
// ...
this.DataContext=/。。。
}
}
XAML:


可能吗?我该怎么做?

我想用字典(或其他简单的东西)

我的变量字典来自数据库,我有大约500个变量要绑定

这些变量不像“人员列表”之类的东西。它们有非常不同的含义,我想用它们作为“动态属性”

我需要将任何变量与任何控件的任何属性绑定。

<ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding dictionary}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
为此,您应该将“dictionary”设置为公共属性,并设置
this.DataContext=this或另一种设置
dictionaryItemsControl.ItemsSource=dictionary

您可以使用

案例1

C#代码:

C#Dic.xaml.cs的代码

class DicViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Dictionary<string, object> dict = new Dictionary<string, object>();

    public Dictionary<string, object> Dict
    {
        get { return dict; }
        set
        {
            dict = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dict"));
        }
    }

    public DicVM()
    {
        Dict["field1"] = 100;
        Dict["field2"] = "Pete Brown";
        Dict["field3"] = new DateTime(2010, 03, 08);
    }

}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new DicViewModel();
    }
}
XAML绑定:

<TextBlock Text="{Binding [field1], Mode=TwoWay}" />
<TextBlock Text="{Binding Dict[field1], Mode=TwoWay}" />


我想我不能像这样使用数据模板。事实上,我的控件不仅仅是标签。对于这个问题,我过于简化了XAML。我正在更新它。@Kamil,你觉得呢?您的具体要求是什么,请发布。当我更新字典中的一个值或向其中添加新项时,此viewmodel将通知控件?@Kamil-Case#2将更新该值。在这种情况下,我使用的是
INotifyPropertyChanged
。setter不会在字典KeyValuePair更改时触发(如案例2)。Dict.Add()、Dict.Remove()也不会触发setter。只有使用新字典的作业才能做到这一点。
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new DicViewModel();
    }
}
<TextBlock Text="{Binding Dict[field1], Mode=TwoWay}" />