我有一个问题,当我进行的比较可以包含单引号或撇号(以及双引号)时,我需要能够在 XAML 绑定(bind)中使用 XPath
这是我的 XAML - 我正在尝试将文本绑定(bind)为元素的 Value 属性,该元素的 Name 属性为“Field ' end”:
<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(似乎不可用,或任何其他功能),使用\' 转义,只使用 ' 等。似乎没有任何效果。有什么想法吗?
对于某些背景,我没有能力更改数据机制。用户数据将始终以 XML 形式作为 XmlDataProvider 提供。
我能够用双引号做类似的事情:
<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>
在这种情况下有没有办法转义单引号?
最佳答案
要使用 XPath
搜索一些包含引号的值,您必须使用 Binding 的元素语法,以便可以将 XPath
设置为这样的属性:
<TextBlock>
<TextBlock.Text>
<Binding Source="{StaticResource UserData}"
XPath="Fields/Field[@Name="Field ' end"]/@Value"/>
</TextBlock.Text>
</TextBlock>
关于c# - 在 XPath Binding WPF 中转义单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26396314/