0

in my application i have this code:

<Grid Name="BaseGrid">

        <Grid.Resources>
            <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml" XPath="person" Source="myXml.xml"/>
        </Grid.Resources>
<ComboBox Grid.Column="0" Name="ScenariCombo" IsSynchronizedWithCurrentItem="True"
                  ItemsSource="{Binding Source={StaticResource ScenesXml}}" DisplayMemberPath="@name" />
</Grid>

Suppose my xml is:

<person name="John">
    <address>Some adress here</address>
    <work>Some work</work>
</person>

I'm planning to update several user controls when the selection changes. the problem is that ComboBox.SelectedItem is not a custom object but an XmlNode as the Combobox is binded to an XmlDataSource.
How would you access an inner node ie: address of the SelectedItem item?

andreapier
  • 2,958
  • 2
  • 38
  • 50

1 Answers1

3

I built a little example for you. It works but i am sure you can do better than that.

my Xaml file:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:src="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid Name="BaseGrid">
    <Grid.Resources>
        <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml" XPath="person" Source="myXml.xml"/>
        <src:Xml2AdressConverter x:Key="Xml2AdressConv"/>
    </Grid.Resources>
    <ComboBox Name="ScenariCombo" IsSynchronizedWithCurrentItem="True"
              ItemsSource="{Binding Source={StaticResource ScenesXml}}" DisplayMemberPath="@name" Margin="0,0,272,264" />
    <Label Content="Address" Height="28" HorizontalAlignment="Left" Margin="12,110,0,0" Name="label1" VerticalAlignment="Top" Width="87" />
    <Label Content="Work" Height="28" HorizontalAlignment="Left" Margin="12,144,0,0" Name="label2" VerticalAlignment="Top" Width="87" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="134,115,0,0" Name="AddressTbx" VerticalAlignment="Top" Width="332" 
             Text="{Binding ElementName=ScenariCombo, Path=SelectedItem, Converter={StaticResource Xml2AdressConv}, ConverterParameter=address}"/>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="134,149,0,0" Name="WorkTbx" VerticalAlignment="Top" Width="332"
              Text="{Binding ElementName=ScenariCombo, Path=SelectedItem, Converter={StaticResource Xml2AdressConv}, ConverterParameter=work}"/>
</Grid>

my Xml2AddressConverter class

class Xml2AdressConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        XmlElement xmlElt = value as XmlElement;

        if (xmlElt != null)
        {
            string str, attName;
            XElement xElt;

            attName = parameter as string;

            xElt= XElement.Load(xmlElt.CreateNavigator().ReadSubtree());
            str = "";
            foreach (XElement x in xElt.Descendants(attName))
            {
                 str = x.Value;
            }
            return str;
        }
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Have a look to this it shows a little bit how to query data from xml stuff using LINQ to XML.

Community
  • 1
  • 1
Shomron
  • 451
  • 4
  • 13