WPF:绑定到组合框SelectedItem

WPF:绑定到组合框SelectedItem,wpf,binding,combobox,Wpf,Binding,Combobox,我有一个带ComboBox的UserControl,它基于XML数据: <Root> <Node Background="Yellow" Foreground="Cyan" Image="1.ico" Property="aaaa" Value="28" /> <Node Background="SlateBlue" Foreground="Black" Image="2.ico" Property="bbbb" Value="2.5" /> <Node

我有一个带ComboBox的UserControl,它基于XML数据:

<Root>
<Node Background="Yellow" Foreground="Cyan" Image="1.ico" Property="aaaa" Value="28" />
<Node Background="SlateBlue" Foreground="Black" Image="2.ico" Property="bbbb" Value="2.5" />
<Node Background="Teal" Foreground="Green" Image="3.ico" Property="cccc" Value="4.0" />
<Node Background="Yellow" Foreground="Red" Image="4.ico" Property="dddd" Value="0" /></Root>

以下是UserControl XAML:

<UserControl x:Class="xxxxxxxx.MyComboBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Name="myComboBoxControl">
<UserControl.Resources>
    <DataTemplate x:Key="dataTemplateNode">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
            </Grid.ColumnDefinitions>
            <Border Background="{Binding XPath=@Background}" Grid.Column="0">
                <Image Source="{Binding XPath=@Image}" 
                       Width="16" 
                       Height="16" 
                       Margin="3" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="1">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3"
                           Text="{Binding XPath=@Property}" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="2">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3" 
                           FontWeight="Bold"
                           Text="{Binding XPath=@Value}" />
            </Border>
        </Grid>
    </DataTemplate>

    <XmlDataProvider x:Key="xmlNodeList" 
                     Source="/data/Combo.xml" 
                     XPath="/Root/Node"/>
</UserControl.Resources>

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding Source={StaticResource xmlNodeList}}" 
          ItemTemplate="{StaticResource dataTemplateNode}"
          HorizontalContentAlignment="Stretch" /></UserControl>

在MainForm.xaml中,我有一个要绑定到my UserControl SelectedItem的文本框

<StackPanel Orientation="Horizontal">
<local:MyComboBox1 x:Name="comboBoxST" />
<TextBox x:Name="textBoxST"/></StackPanel>

如果你能指导我怎么做,我会很高兴的


提前谢谢

这里的诀窍是,当您必须绑定到绑定到XML的ItemControl上的SelectedItem时,所选项本身就是一个XmlElement,您必须使用XPath来获取所需的元素/属性

实现这一点的最简单方法是使用DataContext:

<TextBox x:Name=textBoxST 
    DataContext="{Binding ElementName=comboBoxST, Path=SelectedItem}" 
    Text="{Binding XPath=@Value}"/>

上面发布的答案是针对直接放置在表单上的列表框的情况。在UserControl和模板化ComboBox的情况下,我会避免纯xml绑定——太多的因素会破坏它。相反,请使用以下代码创建依赖项属性:

  public MyComboBox()
    {
        InitializeComponent();
        myComboBox.SelectionChanged += MyComboBoxSelectionChanged;
    }

    void MyComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SetValue(SelValueProperty, ((XmlElement)e.AddedItems[0]).Attributes["Value"].Value);
    }

    public static readonly DependencyProperty SelValueProperty =
        DependencyProperty.Register("SelValue", typeof(string), typeof(MyComboBox),
            new FrameworkPropertyMetadata(""));
那么绑定就很简单了:

<TextBox x:Name=textBoxST Text="{Binding ElementName=comboBoxST, Path=SelValue}"/>

无论如何,我更喜欢谢尔盖早期的方法。然而,在我的场景中,我有一个标签而不是一个文本框,但这对我来说很有效:

    <Label x:Name="labelST" Content="{Binding ElementName=comboBoxST, Path=SelectedValue}"/>


斯帕西巴,谢尔盖。

你好,萨尔多霍夫!感谢您的回复,但不幸的是,您的解决方案不起作用:-(.可能是因为原始ComboBox的XML绑定被封装到UserControl中?在Silverlight 5中为我工作,而不必指定
XPath
,只需绑定到目标持有对象的属性名,例如
Text={binding Description}