开始之前先了解一些基础知识:
Combox
DisplayMemberPath属性,这个属性就是用于界面显示的数据类型,可以将集合元素中的一个属性类型赋值给DisplayMemberPath属性。
SelectedValuePath属性,用于指定在集合元素中被选中的值的类型,对应的SelectedValue属性的值就是就是这个指定的类型的值。
触发器
CommandParameter 传参
ElementName 绑定元素的名称
上代码:
一、事件
public class DeleteCommand : ICommand
{
private Action<object> executeAction;
private Func<object, bool> canExecuteFunc;
public DeleteCommand(Action<object> execute)
: this(execute, null)
{ }
public DeleteCommand(Action<object> execute, Func<object, bool> canExecute)
{
if (execute == null)
{
return;
}
executeAction = execute;
canExecuteFunc = canExecute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (canExecuteFunc == null)
{
return true;
}
return canExecuteFunc(parameter);
}
public void Execute(object parameter)
{
if (executeAction == null)
{
return;
}
executeAction(parameter);
}
}
二、数据绑定
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChange([CallerMemberName] string propertyName=null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MainVM : ViewModelBase
{
private bool _isedit;
public bool IsEditabl
{
get { return _isedit; }
set { _isedit = value;
NotifyPropertyChange();
}
}
public List<ComboBoxItemModel<Book>> list { get; set; }
private List<Book> _dicItem;
private Book selectedModel;
public Book SelectedModel
{
get { return selectedModel; }
set
{
selectedModel = value;
NotifyPropertyChange();
Console.WriteLine($"{ value.BookName} {value.Salary}");
}
}
public MainVM()
{
IsEditabl = false;
list = new List<ComboBoxItemModel<Book>>
{
new ComboBoxItemModel<Book>{Description="红楼梦",SelectedModel=new Book{ BookName="张三",Salary=10000m },IsEnable=true},
new ComboBoxItemModel<Book>{Description="三国演绎",SelectedModel=new Book{ BookName="李四",Salary=10000m },IsEnable=false},
new ComboBoxItemModel<Book>{Description="水浒转",SelectedModel=new Book{ BookName="赵五",Salary=10000m },IsEnable=true},
new ComboBoxItemModel<Book>{Description="西游记",SelectedModel=new Book{ BookName="孙六",Salary=10000m },IsEnable=false}
};
SelectedModel = list[0].SelectedModel;
}
public ICommand MenuSelectionChangedCommand => new DeleteCommand(obj =>
{
IsEditabl = true;
if (obj == null) return;
});
}
public class Book
{
public string BookName { get; set; }
public Decimal Salary { get; set; }
}
public class ComboBoxItemModel<T>
{
public string Description { get; set; }
public T SelectedModel { get; set; }
public bool IsEnable { get; set; }
}
三、xaml界面
<Window x:Class="TestCombox.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:TestCombox"
mc:Ignorable="d"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainVM/>
</Window.DataContext>
<Grid>
<ComboBox
ItemsSource="{Binding list}"
x:Name="Combo"
HorizontalAlignment="Left"
Margin="151,114,0,0"
VerticalAlignment="Top"
Width="193" Height="90"
DisplayMemberPath="Description"
IsEditable="{Binding IsEditabl}"
SelectedValuePath="{Binding SelectedModel}"
>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsEnabled" Value="{Binding IsEnable}" />
</Style>
</ComboBox.ItemContainerStyle>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding MenuSelectionChangedCommand}"
CommandParameter="{Binding ElementName=Combo,Path=SelectedItem}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<i:InvokeCommandAction Command="{Binding MenuSelectionChangedCommand}"
CommandParameter="{Binding ElementName=Combo,Path=Text}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</Grid>
</Window>