[英]Binding to a dictionary - with a key of ' ( '
I have data templates that looks like: 我有数据模板,如下所示:
<DataTemplate>
<TextBlock DataContext="{Binding Fields[ABC]}" Text="{Binding}"/>
</DataTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding Fields[)]}" Text="{Binding}"/>
</DataTemplate>
For a class that looks like 对于一个看起来像的类
class Source {
public Dictionary<string, string> Fields { get; private set; }
}
When applying the second template, with the ')' key in the DataContext binding, I get a XamlParseException. 当应用第二个模板时,在DataContext绑定中使用')'键,我得到一个XamlParseException。 Is there any way to allow Dictionary Binding to work with other strings such as ')' ?
有没有办法让Dictionary Binding与其他字符串一起工作,比如')'? Some sort of escape character sequence?
某种逃避字符序列?
You could construct a valid path by using path parameters , this makes sure the parenthesis is passed as a string and not part of the path description. 您可以使用路径参数构造有效路径,这可确保括号作为字符串传递,而不是路径描述的一部分。 The easiest way to do that would probably be via a custom markup extension as shown in this answer of mine.
最简单的方法可能是通过自定义标记扩展,如我的这个答案所示。
The binding then could be written as: 然后绑定可以写成:
{Binding Path={me:PathConstructor Fields[(0)],')'}}
(Quotes around the parenthesis are optional but make it more readable i think) (括号内的引号是可选的,但我认为它更具可读性)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.