The Code is as follows:
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>
MainWindow. xaml. cs
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. In XmlDataProvider, you must declare xmlns = ""; otherwise, the XPath search path will become invalid;
2. To make ComboBox editable, set IsEditable = "True ".
Vs2010 Solution download: http://download.csdn.net/detail/xinyaping/4356561