wpfcomboxmvvm

开始之前先了解一些基础知识:

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>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值