[英]Getting the actual value of an XML binding in C# from XAML
Here's my XAML 这是我的XAML
<Label x:Name="fileName" Content="{Binding XPath=./name}" MouseDown="copyUrl" />
and here's my C# code 这是我的C#代码
private void copyUrl(object sender, System.Windows.RoutedEventArgs e)
{
Label lol = (Label)sender;
string fileUrl = lol.Content.ToString();
MessageBox.Show(fileUrl);
}
I expect the output to be data.txt
but I get System.Xml.XmlElement
instead! 我希望输出是
data.txt
但是我却得到System.Xml.XmlElement
! What am I casting wrong or missing here? 我在这里丢错什么了吗?
You need to convert your content from the XmlElement
to a string or access its InnerXml
property. 您需要将内容从
XmlElement
转换为字符串或访问其InnerXml
属性。 It is just doing an implicit ToString()
on the bound item. 它只是在绑定项上执行隐式
ToString()
。
string fileUrl = ((sender as Label).Content as XmlElement).InnerText;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.