【发布时间】:2014-10-16 04:39:36
【问题描述】:
当我进行的比较中可以有单引号或撇号(以及双引号)时,我需要能够在 XAML 绑定中使用 XPath
这是我的 XAML - 我正在尝试将 Text 绑定为元素的 Value 属性与“Field ' end”的 Name 属性:
<Canvas Width="9.5cm" Height="6.5cm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="RootCanvas2">
<Canvas.Resources>
<XmlDataProvider x:Key="UserData" XPath="User" Source="MockUser.xml"></XmlDataProvider>
</Canvas.Resources>
<Viewbox StretchDirection="DownOnly" VerticalAlignment="Center" HorizontalAlignment="Left" Canvas.Top="1.0cm" Canvas.Left="0.0cm" Width="3.0cm" Height="0.8cm" >
<TextBlock Text="{Binding Source={StaticResource UserData}, XPath=Fields/Field[@Name\=\'Field \' end\']/@Value}" />
</Viewbox>
</Canvas>
下面是我正在使用的 XML 示例。
<?xml version="1.0" encoding="utf-8" ?>
<User>
<Fields>
<Field Name="Last Name" Value="Smith" />
<Field Name="First Name" Value="John" />
<Field Name="Personnel ID" Value="000001" />
<Field Name="Field "quote"" Value="000021" />
<Field Name="Field ' end" Value="000056" />
</Fields>
</User>
问题是我无法在 XAML 中包含的 XPath 中正确转义 '。
我尝试过使用 concat (似乎不可用,或任何其他功能),使用 \' 转义,仅使用 ' 等。似乎没有任何效果。有什么想法吗?
对于某些背景,我没有能力更改数据的机制。用户数据将始终作为 XmlDataProvider 以 XML 形式提供。
我可以用双引号来做类似的事情:
<Viewbox StretchDirection="DownOnly" VerticalAlignment="Center" HorizontalAlignment="Left" Canvas.Top="0.0cm" Canvas.Left="0.0cm" Width="3.0cm" Height="0.8cm" >
<TextBlock Text="{Binding Source={StaticResource UserData}, XPath=Fields/Field[@Name\=\'Field \"quote\"\']/@Value}" />
</Viewbox>
有没有办法在这种情况下转义单引号?
【问题讨论】: