前言
wpf中ComboBox的应用非常广泛,本文就来介绍ComboBox在wpf中的应用。
1、非MVVM模式下
1.1 xaml添加元素
csharp
<ComboBox x:Name="cbx_test1" SelectedIndex=" 0" >
<ComboBoxItem >小明</ComboBoxItem >
<ComboBoxItem >小王</ComboBoxItem >
<ComboBoxItem >小陈</ComboBoxItem >
</ComboBox >

1.2 代码添加元素
MainWindow是新建wpf程序时的主窗体类
csharp
public MainWindow()
{
InitializeComponent();
cbx_test1.Items.Add("小李");
}

1.3 将ComboBox选中的值赋值给string类型的变量
1.3.1 针对xaml添加的元素
直接添加事件SelectionChanged
csharp
<ComboBox x:Name="cbx_test1" SelectedIndex=" 0" SelectionChanged="cbx_test1_SelectionChanged">
<ComboBoxItem >小明</ComboBoxItem >
<ComboBoxItem >小王</ComboBoxItem >
<ComboBoxItem >小陈</ComboBoxItem >
</ComboBox >
这里需要注意的是利用xaml的ComboBoxItem添加的成员,必须先转换为System .Windows .Controls .ComboBoxItem类型,然后调用Content属性才可以。
csharp
private void cbx_test1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string personName =((System .Windows .Controls .ComboBoxItem ) cbx_test1.SelectedItem).Content .ToString ();
Console.WriteLine(personName);
}
1.3.2 针对代码添加的元素
csharp
<ComboBox x:Name="cbx_test1" SelectedIndex=" 0" SelectionChanged="cbx_test1_SelectionChanged">
</ComboBox >
csharp
public MainWindow()
{
InitializeComponent();
cbx_test1.Items.Add("小李");
cbx_test1.Items.Add("小王");
cbx_test1.Items.Add("小明");
}
对于通过代码添加的成员,可以直接调用ToString ()
csharp
private void cbx_test1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string personName = cbx_test1.SelectedItem .ToString ();
Console.WriteLine(personName);
}
1.4 将ComboBox选中的值赋值给枚举类型的变量
csharp
<ComboBox x:Name="cbx_test1" SelectedIndex=" 0" SelectionChanged="cbx_test1_SelectionChanged">
</ComboBox >
使用字符串转枚举将SelectedItem的值转换为枚举。
csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
cbx_test1.Items.Add("小李");
cbx_test1.Items.Add("小王");
cbx_test1.Items.Add("小明");
}
private void cbx_test1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PersonName personName = (PersonName)Enum.Parse(typeof(PersonName), cbx_test1.SelectedItem.ToString());
}
}
enum PersonName
{
小李,
小王,
小明
}
2、MVVM模式下
2.1 将ComboBox选中的值赋值给string类型的变量
csharp
public class ViewModelBase
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
}
csharp
public class ViewModel : ViewModelBase
{
public ViewModel()
{
Items = new List<string>();
Items.Add("分时1");
Items.Add("分时2");
}
private List<string> items;
public List<string> Items
{
get
{
return items;
}
set
{
this.items = value;
OnPropertyChanged("Items");
}
}
private string selectItem = "分时1";
public string SelectItem
{
get
{
return selectItem;
}
set
{
this.selectItem = value;
OnPropertyChanged("SelectItem");
Console.WriteLine(SelectItem);
}
}
}
csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
xaml界面代码
csharp
<ComboBox x:Name="cbx_test2" ItemsSource="{ Binding Path=Items}" SelectedItem ="{Binding SelectItem}">
</ComboBox >
2.2 将ComboBox选中的值赋值给枚举类型的变量
这里使用ComboBox的SelectedIndex,由于需要绑定ViewModel中的枚举类型,但是这两个类型又不匹配,所以要加上类型转换。
csharp
public class DivisionTypeToInt : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DivisionType divisionType = (DivisionType)value;
return (int)divisionType;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int v = (int)value;
DivisionType divisionType=DivisionType.Time1 ;
switch (v)
{
case 0:
divisionType= DivisionType.Time1;
break;
case 1:
divisionType= DivisionType.Time2;
break;
}
return divisionType;
}
}
csharp
<Window x:Class="wpf之combox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf之combox" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:DivisionTypeToInt x:Key="divisionTypeToInt"/>
</Window.Resources>
<Grid>
<StackPanel >
<ComboBox x:Name="cbx_test2" ItemsSource="{ Binding Path=Items}" SelectedIndex="{Binding divisionType, Converter={StaticResource divisionTypeToInt}}">
</ComboBox >
</StackPanel >
</Grid>
</Window>
csharp
public class ViewModelBase
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
}
csharp
public class ViewModel : ViewModelBase
{
public ViewModel()
{
Items = new List<string>();
Items.Add("分时1");
Items.Add("分时2");
}
private List<string> items;
public List<string> Items
{
get
{
return items;
}
set
{
this.items = value;
OnPropertyChanged("Items");
}
}
private DivisionType _divisionType =DivisionType.Time1 ;
public DivisionType divisionType
{
get
{
return _divisionType;
}
set
{
this._divisionType = value;
OnPropertyChanged("divisionType");
}
}
}
csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
combox中有分时1、分时2两个选项。