WPF:绑定(bind)到 ComboBox SelectedItem

标签 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>

这是用户控件 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 中,我有一个要绑定(bind)到我的 UserControl SelectedItem 的 TextBox。
<StackPanel Orientation="Horizontal">
<local:MyComboBox1 x:Name="comboBoxST" />
<TextBox x:Name="textBoxST"/></StackPanel>

如果你能指导我如何做到这一点,我会很高兴。

提前致谢!

最佳答案

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

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

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

关于WPF:绑定(bind)到 ComboBox SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/840407/

相关文章:

WPF ComboBox 绑定(bind)行为

c# - 使用属性... INotifyPropertyChanged

C#执行cmd命令不起作用

javascript - Angularjs 两种方式绑定(bind)

wpf - 如何为用户控件创建 MVVM 而不将所有内部结构暴露给消费者?

wpf - 公开 WPF 中绑定(bind)的内部 Control 属性

java - 如何使用另一个组合框中的选择来填充组合框? JavaFX

javascript - 如何在 Ext JS 中将组合框中的菜单向左侧对齐

wpf - TargetType 与元素类型不匹配

wpf - 在 XAML 中绑定(bind)到图像时处理 null