Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hashtable as DataContext (for WPF/XAML Binding)

In code I have a Hashtable named MyHashtable. This Hashtable contains an element with key="Value", value=3. I'm currently trying to bind this value to a textbox. 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, while the first binding works just great?

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


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

like image 882
Rudey Avatar asked Aug 29 '26 08:08

Rudey


1 Answers

For second option you can just use this:

<TextBlock Margin="4" 
     DataContext="{Binding MyHashtable}" 
     Text="{Binding RelativeSource={x:Static RelativeSource.Self},
            Path=DataContext[Value]}" />
like image 91
Woodman Avatar answered Sep 01 '26 03:09

Woodman