Using Hashtable as DataContext (for WPF/XAML Binding)

风流意气都作罢 提交于 2019-12-23 17:53:16

问题


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.


回答1:


For second option you can just use this:

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


来源:https://stackoverflow.com/questions/14623728/using-hashtable-as-datacontext-for-wpf-xaml-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!