2016-07-08 75 views
1

比方說,我有一些字典,我想將這個字典中的項目綁定到一些控件,我想通過項目鍵綁定。通過鍵綁定字典項目

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個變量綁定。

這些變量不像「人員列表」或類似的東西。它們具有非常不同的含義,我想用它們作爲「動態屬性」。

我需要綁定任何變量與任何控件的任何屬性。

回答

1
<ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding dictionary}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Key}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

對於你應該做 '字典' 公共財產,並設置this.DataContext = this;或備選地,設置dictionaryItemsControl.ItemsSource = dictionary;

+0

我想我不能使用數據模板這樣。事實上,我的控件不僅僅是標籤。這個問題我簡化了我的XAML。我在更新它。 – Kamil

+0

@Kamil你認爲?你確切的要求是什麼,請發佈。 – AnjumSKhan

0

您可以使用

CASE#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}" /> 

CASE#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}" /> 
+0

當我更新字典中的一個值或向其添加新項目時,此視圖模型將通知控件? – Kamil

+0

@Kamil - 案例#2將更新該值。在這種情況下,我使用**'INotifyPropertyChanged' **。 –