Using Hashtable as a DataContext (for WPF / XAML binding)

In the code, I have a Hashtable named MyHashtable . This hashtable contains an element with the key = "Value" , value = 3 . I'm currently trying to associate this value with a text box. This is my XAML code:

 <TextBlock Margin="4" Text="{Binding MyHashtable[Value]}" /> <TextBlock Margin="4" DataContext="{Binding MyHashtable}" Text="{Binding [Value]}" /> 

Q: Why does the second binding not work, and the first binding works just fine?

For the second binding, I tried other bindings for the text, such as: Value , this[Value] or even Me[Value] , but all of them did not work.


Using Item[Value] gives me an interesting exception: Parameter count mismatch. Does anyone understand this? This is due to the differences between C # and VB.NET. See this question .

+3
source share
1 answer

For the second option, you can simply use this:

 <TextBlock Margin="4" DataContext="{Binding MyHashtable}" Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=DataContext[Value]}" /> 
+1
source

All Articles