WPF Binding to Xml; how to extract selected XmlElement and cast to Object

寵の児 提交于 2019-12-24 02:16:28

问题


I have a WPF ComboBox bound to an Xml file and XmlDataProvider

<ComboBox Canvas.Left="980"
          Canvas.Top="16"
          DisplayMemberPath="Name"
          FontSize="45"
          Height="76"
          SelectionChanged="comboBox1_SelectionChanged"
          Padding="10"
          ItemsSource="{Binding Source={StaticResource Sites}, XPath=Site}"
          Name="comboBox1"
          Style="{DynamicResource InfoKioskLargeStyledDropDown}"
          Width="600" />

My XML Looks as so:

<Site>
    <Name>Campus Pride</Name>    
    <Url>CampusPride</Url>
    <SlideCount>10</SlideCount>
</Site>

I then have an object matched up matching my XML:

public class Site
{
   public string Name { get; set; }
   public string Url { get; set; }
   public int SlideCount { get; set; }
}

What I want to do is on ComboBox selection changed, retrieve the object in SelectedItem and cast it to an object of type Site. The problem is that the combobox1.SelectedItem property is an XmlElement. Is there a quick way to convert my XmlElement to a Site object, or do I have to invoke a serializer and do it manually?

来源:https://stackoverflow.com/questions/13960278/wpf-binding-to-xml-how-to-extract-selected-xmlelement-and-cast-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!