按键与字典项绑定

时间:2016-07-08 10:15:36

标签: wpf dictionary binding

让我说我有一些字典,我想将这个字典中的项目绑定到某些控件,我想按项目键绑定。

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 = //...
    }
}

XAML:

<Window ... >
    <Grid>
        <Label Content="{Binding ...}"/><!-- "Item 1" -->
        <TextBox Text="{Binding ...}"/><!-- "Item 3" -->

        <StackPanel>
             <CustomControl CustomProperty="{Binding ...}"/><!-- "Item 77" -->
             <CustomControl2 CustomProperty="{Binding ...}"/><!-- "Item 99" -->
        </StackPanel>
    </Window>

有可能吗?我该怎么办?

我想使用字典(或类似的东西)。

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

这些变量不像&#34;人员名单&#34;或类似的东西。它们具有非常不同的含义,我想将它们用作&#34;动态属性&#34;。

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

2 个答案:

答案 0 :(得分:2)

p.animateme {
animation-delay:  1.5s;
-webkit-animation-delay:  1.5s;  

-webkit-animation-duration:  3s;
animation-duration:  3s;

-webkit-animation-name:  zoomOutDown;
animation-name:  zoomOutDown;

-webkit-animation-fill-mode: both;
animation-fill-mode: both;

animation-iteration-count: 1;
-webkit-animation-iteration-count: 1;

}

为此你应该制作&#39;字典&#39;公共属性,并设置<ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding dictionary}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Key}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 或者设置this.DataContext = this;

答案 1 :(得分:0)

您可以使用

案例#1

C#代码:

public partial class MainWindow : Window
{
    Dictionary<string, object> _data = new Dictionary<string, object>();

    public MainWindow()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {

        _data["field1"] = 100;
        _data["field2"] = "Pete Brown";
        _data["field3"] = new DateTime(2010, 03, 08);

        this.DataContext = new DicVM();

    }
}

XAML绑定:

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

案例#2

C#代码: DicViewModel.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);
    }

}

C#代码 Dic.xaml.cs

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 Dict[field1], Mode=TwoWay}" />