简体   繁体   中英

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM