1.xaml檔案
<ComboBox x:Name="cmbDepartmentID" Grid.Row="1" Width="200"
Margin="4" Height="25"
ItemsSource="{Binding Path=PersonList}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=CurrentItem, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding CommandQuery}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
2.資料來源PersonList,當前選中的CurrentItem
ObservableCollection<Person> personList;
public ObservableCollection<Person> PersonList
{
get { return personList; }
set { personList = value; }
}
建構函式裡面添加資料來源
public MainPageViewModel()
{
personList = new ObservableCollection<Person>();
personList.Add(new Person() { Id = 1, Name = "張三", Address = "東京" });
personList.Add(new Person() { Id = 2, Name = "李四", Address = "洛陽" });
personList.Add(new Person() { Id = 3, Name = "王五", Address = "神都" });
personList.Add(new Person() { Id = 4, Name = "趙六", Address = "帝都" });
personList.Add(new Person() { Id = 5, Name = "錢七", Address = "魔都" });
}
當前選中的CurrentItem
string currentItem;
public string CurrentItem
{
get { return currentItem; }
set
{
if (!ReferenceEquals(currentItem, value))
{
currentItem = value;
this.RaisePropertyChanged(() => this.CurrentItem);
}
}
}
3.添加事件
private ICommand commandQuery;
public ICommand CommandQuery
{
get { return commandQuery ?? (commandQuery = new DelegateCommand(this.MethodQuery)); }
}
4.當選中其中一個的時候,進行的操作
private void MethodQuery()
{
//當選中其中一個的時候,進行的操作
MessageBox.Show(CurrentItem);
}