Getting the actual value of an XML binding in C# from XAML
Here's my XAML
<Label x:Name="fileName" Content="{Binding XPath=./name}" MouseDown="copyUrl" />
开发者_开发百科and here's my C# code
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! 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. It is just doing an implicit ToString()
on the bound item.
string fileUrl = ((sender as Label).Content as XmlElement).InnerText;
精彩评论