WPF:绑定到ComboBox SelectedItem

use*_*493 6 wpf binding combobox

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

<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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

在MainForm.xaml中,我有一个TextBox,我想绑定到我的UserControl SelectedItem.

<StackPanel Orientation="Horizontal">
<local:MyComboBox1 x:Name="comboBoxST" />
<TextBox x:Name="textBoxST"/></StackPanel>
Run Code Online (Sandbox Code Playgroud)

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

提前致谢!

Ser*_*hov 13

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

实现此目的的最简单方法是使用DataContext:

<TextBox x:Name=textBoxST 
    DataContext="{Binding ElementName=comboBoxST, Path=SelectedItem}" 
    Text="{Binding XPath=@Value}"/>
Run Code Online (Sandbox Code Playgroud)


小智 5

就其价值而言,我更喜欢 Sergey 早期的方法。但是,在我的场景中,我有一个标签而不是文本框,但这对我有用:

    <Label x:Name="labelST" Content="{Binding ElementName=comboBoxST, Path=SelectedValue}"/>
Run Code Online (Sandbox Code Playgroud)

斯帕西巴,谢尔盖。