使用哈希表的DataContext(为WPF / XAML绑定) [英] Using Hashtable as DataContext (for WPF/XAML Binding)
问题描述
在code我有一个名为Hashtable的 MyHashtable
。这个哈希表包含键= 价值的元素
,值= 3
。我目前正在为这个值到文本框绑定。这是我的XAML code:
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.
使用第[超值]
给我一个有趣的例外:参数数量不匹配
<击>是否有人理解?击>这是因为C#和VB.NET之间的差异。见<一href=\"http://stackoverflow.com/questions/14642503/binding-to-multiple-indexed-properties-in-net\">this问题。
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.
推荐答案
有关第二个选项你可以使用这样的:
For second option you can just use this:
<TextBlock Margin="4"
DataContext="{Binding MyHashtable}"
Text="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=DataContext[Value]}" />
这篇关于使用哈希表的DataContext(为WPF / XAML绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!