开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜