【问题标题】:WPF: Binding to ComboBox SelectedItemWPF:绑定到 ComboBox SelectedItem
【发布时间】:2010-10-24 20:01:24
【问题描述】:

我有一个基于 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>

这是 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 中,我有一个要绑定到我的 UserControl SelectedItem 的 TextBox。

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

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

提前致谢!

【问题讨论】:

    标签: wpf binding combobox


    【解决方案1】:

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

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

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

    【讨论】:

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

    上面发布的答案是针对直接放在表单上的列表框的情况。在 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}"/>
    

    【讨论】:

      【解决方案3】:

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

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

      斯帕西巴,谢尔盖。

      【讨论】:

        猜你喜欢
        • 2011-11-01
        • 2015-06-28
        • 1970-01-01
        • 2021-10-18
        • 2017-11-09
        • 2016-03-25
        • 2019-10-01
        • 2012-06-23
        • 2011-03-07
        相关资源
        最近更新 更多