代码如下:
MainWindow.xaml
<Window x:Class="ComboBoxDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="80" Width="329">
<StackPanel Orientation="Horizontal" Width="304">
<StackPanel.Resources>
<XmlDataProvider x:Key="dataMemberList" XPath="/MemberList">
<x:XData>
<MemberList xmlns="">
<Member>
<Name>Stephone</Name>
<Id>54765</Id>
</Member>
<Member>
<Name>Alex</Name>
<Id>32768</Id>
</Member>
<Member>
<Name>Smith</Name>
<Id>44326</Id>
</Member>
</MemberList>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<Label Height="26">Choose/Input member name: </Label>
<ComboBox Name="memberNameEditor" Height="24" Width="100"
ItemsSource="{Binding Source={StaticResource dataMemberList}, XPath=Member}"
DisplayMemberPath="Name"
IsEditable="True" />
<Button Height="24" Margin="4,0,4,0"
Content="OK"
Click="ButtonOK_Click"/>
</StackPanel>
</Window>
using System.Windows;
namespace ComboBoxDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this.memberNameEditor.Text);
}
}
}
Notes:
1. 在XmlDataProvider中,必须申明 xmlns="",否则XPath搜索路径会失效;
2. 为了使ComboBox可编辑,需设置 IsEditable="True"。
Vs2010 Solution download: http://download.csdn.net/detail/xinyaping/4356561