WPF ComboBox Command Binding
来源:互联网 发布:linux下类似fildder 编辑:程序博客网 时间:2024/06/05 04:46
控件里面有个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
- WPF ComboBox Command Binding
- [WPF] Binding Enum to ComboBox
- WPF binding
- WPF BINDING
- WPF Binding
- WPF Binding
- WPF ComboBox
- ComboBox Binding - data source
- wpf command
- WPF command
- WPF data-binding
- WPF(Binding)
- WPF(Binding of ItemsControl)
- WPF(Binding of LinQ)
- WPF(Binding of ObjectDataProvider)
- WPF(Binding of RelativeSource)
- 【WPF】binding的使用
- WPF TreeView Binding
- 读取24位 BMP 图像并生成 JPG 缩略图
- Android Camera 相机常见问题
- 今天第一天来,很喜欢这个论坛啊
- 活动生命周期
- TCP协议从客户端传送文件给服务端
- WPF ComboBox Command Binding
- 忘记Win XP登入密码的常用解决办法
- C,C++中的static
- ksyun s3 签名计算 php 代码
- Java原来如此-反射机制
- oracle startup 之后 如果执行SQL 那么立马shutdown 原因分析
- 关于Java读取和编写BMP文件的总结
- Android中View内部代码探析
- 算法导论之插入排序算法