WPF:绑定到ComboBox SelectedItem

编程入门 行业动态 更新时间:2024-10-21 06:44:48
本文介绍了WPF:绑定到ComboBox SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a UserControl with ComboBox that based on XML data:

<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:

Here is the UserControl XAML:

<UserControl x:Class="xxxxxxxx.MyComboBox" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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.

In the MainForm.xaml I have a TextBox that I want to bind to the my UserControl SelectedItem.

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

如果您能指导我该怎么做,我会很高兴.

I will glad if you will guid me how to do that.

提前谢谢!

推荐答案

此处的窍门是,当您必须绑定到绑定到XML的ItemControl上的SelectedItem时,所选项目本身就是XmlElement,并且必须使用XPath以获得所需的元素/属性.

The trick here is that when you have to bind to the SelectedItem on an ItemControl bound to XML, the selected item itself is an XmlElement, and you have to use XPath to get to the needed element/attribute.

最简单的方法是使用DataContext:

The easiest way to achieve this is to use DataContext:

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

更多推荐

WPF:绑定到ComboBox SelectedItem

本文发布于:2023-11-06 14:17:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   WPF   SelectedItem   ComboBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!