WPF EventSetter Handler Command
2016-07-03 10:46
477 查看
最近做一个工具,突然发现ListBox和ListView等列表控件的MouseDoubleClick事件有时候是获取不到当前双击的行对象数据的,比如这样写:
View Code
<Setter Property="localCommand:CommandBehavior.Command" Value="{Binding DataContext.DoubleCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WinTest}}}"></Setter>
关于这个Command的Value绑定要使用FindAncestor进行查找才能解决,不然是绑定不到ViewModel中的DoubleCommand
发个图看看:
关于CommandBehavior代码可以在
http://download.csdn.net/download/doncle000/7029327 下载使用
国外博客http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/
对于SimpleCommad.cs的源文件我增加了两个参数的构造函数:
源代码下载地址
using AttachedCommandBehavior; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfTest { /// <summary> /// WinTest.xaml 的交互逻辑 /// </summary> public partial class WinTest : Window { ViewModel VModel = new ViewModel(); public WinTest() { InitializeComponent(); this.DataContext = VModel; } private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListBox listBox = sender as ListBox; if (listBox == null || listBox.SelectedItem == null) { MessageBox.Show("ListBox1双击对象为空..."); } else { var model = listBox.SelectedItem as ListBoxModel; MessageBox.Show("当前对象为" + model.Name + " " + model.Age); } } private void ListBox2_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListBoxItem listBoxItem = sender as ListBoxItem; if (listBoxItem == null) { MessageBox.Show("ListBox2双击对象为空..."); } else { ListBoxModel model = listBoxItem.Content as ListBoxModel; if (model != null) { VModel.CurrentSelectItem2 = listBoxItem.Content as ListBoxModel; MessageBox.Show(model.Name + " " + model.Age); } } } } public class ViewModel : INotifyPropertyChanged { public ViewModel() { for (int i = 0; i < 5; i++) { DataList.Add(new ListBoxModel() { Name = "张三" + i.ToString(), Age = 1000 + i }); DataList2.Add(new ListBoxModel() { Name = "李四" + i.ToString(), Age = 100 + i }); } doubleCommand = new SimpleCommand(obj => { ListBoxItem listBoxItem = obj as ListBoxItem; if (listBoxItem != null) { ListBoxModel model = listBoxItem.Content as ListBoxModel; if (model != null) { CurrentSelectItem2 = model; MessageBox.Show("Command Banding" + model.Name + " " + model.Age); } } //wpftest.ViewModel MessageBox.Show("Cmd..."); }, o => true); } public SimpleCommand DoubleCommand { get { return doubleCommand; } set { doubleCommand = value; //OnPropertyChanged(new PropertyChangedEventArgs("DoubleCommand")); } } private ObservableCollection<ListBoxModel> dataList = new ObservableCollection<ListBoxModel>(); private ObservableCollection<ListBoxModel> _dataList2 = new ObservableCollection<ListBoxModel>(); private ListBoxModel _CurrentSelectItem; private ListBoxModel _CurrentSelectItem2; private SimpleCommand doubleCommand; public ObservableCollection<ListBoxModel> DataList { get { return dataList; } set { dataList = value; } } /// <summary> /// 当前双击的对象 /// </summary> public ListBoxModel CurrentSelectItem { get { return _CurrentSelectItem; } set { _CurrentSelectItem = value; OnPropertyChanged(new PropertyChangedEventArgs("CurrentSelectItem")); } } /// <summary> /// ListBox2双击的对象 /// </summary> public ListBoxModel CurrentSelectItem2 { get { return _CurrentSelectItem2; } set { _CurrentSelectItem2 = value; OnPropertyChanged(new PropertyChangedEventArgs("CurrentSelectItem2")); } } public ObservableCollection<ListBoxModel> DataList2 { get { return _dataList2; } set { _dataList2 = value; } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } } public class ListBoxModel : INotifyPropertyChanged { /// <summary> /// 姓名 /// </summary> private string _Name; /// <summary> /// 年龄 /// </summary> private int _Age; public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); } } public int Age { get { return _Age; } set { _Age = value; OnPropertyChanged(new PropertyChangedEventArgs("Age")); } } public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } public event PropertyChangedEventHandler PropertyChanged; } }
View Code
<Setter Property="localCommand:CommandBehavior.Command" Value="{Binding DataContext.DoubleCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WinTest}}}"></Setter>
关于这个Command的Value绑定要使用FindAncestor进行查找才能解决,不然是绑定不到ViewModel中的DoubleCommand
发个图看看:
关于CommandBehavior代码可以在
http://download.csdn.net/download/doncle000/7029327 下载使用
国外博客http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/
对于SimpleCommad.cs的源文件我增加了两个参数的构造函数:
public SimpleCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); CanExecuteDelegate = canExecute; ExecuteDelegate = execute; }
源代码下载地址
相关文章推荐
- NASA 美国国家航空航天局开源项目列表
- NLTK-自然语言工具包
- xamarin android webview XHR错误
- storm入门
- TSP问题之状态压缩dp法
- Leetcode add-two-numbers
- 202. Happy Number
- Java千百问_06数据结构(024)_用二进制如何表示整型数值
- Handler消息机制原理
- 线程基础知识系列(五)认识volatile
- HDU 1564 Play a game(博弈找规律)
- 神经网络:卷积神经网络(转)
- socket.io多房间聊天
- PyQt界面编程应用与实践
- 第k个排列
- 华为入职
- Python: 渐进猜数字游戏 <1> 代码、测试与猜数字
- HDU 1525 Euclid's Game(博弈)
- UNIX/Linux 系统实现限制
- PAT (Advanced Level) 1086. Tree Traversals Again (25)