问题
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