WPF ComboBox Command Binding

控件里面有个ComboBox,想要实现ComboBox输入后,输入值可以作为它自己的的Item,这和包含这个ComboBox的控件本身没多大关系........所以想着Command了。

学习了一下,挺好用的。


1、创建WPF 项目,

引用:Microsoft.Expression.Interactivity.DLL,System.Windows.Interactivity;

前台添加: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<Window x:Class="ComboBox_Command_Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:viewModel="clr-namespace:ComboBox_Command_Binding.ViewModel"
        xmlns:command="clr-namespace:ComboBox_Command_Binding.Command"
        Title="MainWindow" Height="350" Width="525" Background="DarkGray">
    <Window.Resources>
        <viewModel:ViewModes x:Key="view"/>
    </Window.Resources>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <ComboBox x:Name="comboBox" Width="200" Height="30" IsEditable="True" VerticalContentAlignment="Center" DataContext="{StaticResource view}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="LostFocus">
                    <command:ExecuteCommandAction Command="{Binding Clicked}" CommandParameter="{Binding ElementName=comboBox}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
        <Button Width="50" Height="30" Margin="5"/>
    </StackPanel>
</Window>

2、ViewModes.cs

using System.Windows.Input;
using Microsoft.Expression.Interactivity.Core;

namespace ComboBox_Command_Binding.ViewModel
{
    using System.Windows.Controls;

    public class ViewModes
    {
        public ViewModes()
        {
            Clicked = new ActionCommand(Click);
        }

        public ICommand Clicked { get; private set; }

        public void Click(object arg)
        {
            var SS = arg as ComboBox;
            if (SS == null || string.IsNullOrWhiteSpace(SS.Text) || SS.Items.Contains(SS.Text)) return;
            SS.Items.Add(SS.Text);
        }
    }
}

3、ExecuteCommandAction.cs

using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Input;
using EventTrigger = System.Windows.Interactivity.EventTrigger;

namespace ComboBox_Command_Binding.Command
{
    public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
    {
        public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
        /// <summary> 命令
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }


        public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));
        /// <summary> 参数
        /// </summary>
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }


        /// <summary>由给定的路由事件触发,参数是事件的发送方
        /// </summary>
        protected override void Invoke(object parameter)
        {
            if (Command == null) return;
            if (Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
        }
    }
}


还有个很不错的文章:http://www.cnblogs.com/matoo/archive/2012/04/14/2447159.html





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值