简体   繁体   English

将几个控件绑定到combobox.selectedItem,它是一个Xml节点

[英]Binding several controls to combobox.selectedItem which is an Xml node

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: 假设我的xml是:

<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. 问题是ComboBox.SelectedItem不是自定义对象,而是XmlNode,因为Combobox绑定到XmlDataSource。
How would you access an inner node ie: address of the SelectedItem item? 您将如何访问内部节点,即SelectedItem项目的地址?

I built a little example for you. 我为您建立了一个小例子。 It works but i am sure you can do better than that. 它有效,但是我相信您可以做得更好。

my Xaml file: 我的Xaml文件:

<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 我的Xml2AddressConverter类

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. 看看到这个它显示一点点如何查询使用LINQ to XML从XML数据的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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