控件里面有个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