MVVM模式下实现拖拽
在文章开始之前先看一看效果图
我们可以拖拽一个"游戏"给ListBox,并且ListBox也能接受拖拽过来的数据, 但是我们不能拖拽一个"游戏类型"给它。
所以当拖拽开始发生的时候我们必须添加一些限制条件,以防止接受不正确的数据。
Item实体
CS
1
2
3
4
|
public class ItemModel : ViewModelBase { public string ItemName { get ; set ; } } |
组实体
CS

public class GroupModel : ViewModelBase
{
/// <summary>
/// 组名
/// </summary>
public string GroupName { get; set; } private int groupCount;
/// <summary>
/// 组数量
/// </summary>
public int GroupCount
{
get { return groupCount; }
set { groupCount = value; base.RaisePropertyChanged("GroupCount"); }
} /// <summary>
/// 子类集合
/// </summary>
public ObservableCollection<ItemModel> ItemModelList { get; set; }
}

给"游戏"实体创建一个模板
XAML
<HierarchicalDataTemplate x:Key="template_Item">
<TextBlock Text="{Binding ItemName}"/>
</HierarchicalDataTemplate>
给"游戏组"实体创建一个模板
XAML

<HierarchicalDataTemplate x:Key="template_Group" ItemsSource="{Binding ItemModelList}" ItemTemplate="{StaticResource template_Item}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding GroupName}"/>
<TextBlock Text="{Binding GroupCount}" Margin="5,0,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>

但是当我准备给TreeView赋值的时候 , 我想起来TreeView的SelectedItem属性不是依赖属性 , 它不支持Binding操作
所以只有自己写一个控件继承TreeView了。为它扩展一个MySelectedItem属性出来。并且重写SelectedItemChange事件
把TreeView的SelectedItem交给扩展的依赖属性MySelectedItem .这样在界面上就可以Binding选中项了
不过由于TreeView各个节点的数据实体可能类型不相同,所以扩展的属性只能定义为object类型
创建自定义树
CS

public class MyTreeView : TreeView
{
public MyTreeView()
{ } /// <summary>
/// 自定义TreeView选中项,支持数据Binding
/// </summary>
public object MySelectItem
{
get { return GetValue(MySelectItemProperty); }
set { SetValue(MySelectItemProperty, value); }
} public static DependencyProperty MySelectItemProperty = DependencyProperty.Register("MySelectItem", typeof(object), typeof(MyTreeView)); /// <summary>
/// 当改变发生时,为自定义的SelectItem属性赋值
/// </summary>
/// <param name="e"></param>
protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
{
if (this.SelectedItem != null)
this.MySelectItem = this.SelectedItem;
base.OnSelectedItemChanged(e);
}
}

XAML
<mc:MyTreeView x:Name="myTree" MouseMove="TreeView_MouseMove" TextBlock.FontSize="14" MySelectItem="{Binding SelectGame,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding GroupSourceList}" ItemTemplate="{StaticResource template_Group}">
</mc:MyTreeView>
CS

private TreeViewItem ti = new TreeViewItem(); private void TreeView_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (myTree.SelectedItem == null)
return;
DragDrop.DoDragDrop(ti, sender, DragDropEffects.Move);
}
}

DragDrop.DoDragDrop方法需要传入一个DependencyObject对象以设置其拖拽时的效果。
但由于TreeView做了数据绑定, 所以它的SelectItem取出来是一个数据实体。而不是一个DependencyObject对象了。
所以我用了一个比较SB的办法就是new一个TreeViewItem。然后设置拖拽移动的效果。
创建ListBox

<ListBox ItemsSource="{Binding GameSourceList}" AllowDrop="true">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="DragEnter">
<Command:EventToCommand Command="{Binding DragEnterCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="DragOver">
<Command:EventToCommand Command="{Binding DragEnterCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="Drop">
<Command:EventToCommand Command="{Binding DropCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>

ViewModel

public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Init();
} #region Properties
/// <summary>
/// 数据源
/// </summary>
public ObservableCollection<GroupModel> GroupSourceList { get; set; } /// <summary>
/// 数据源
/// </summary>
public ObservableCollection<ItemModel> GameSourceList { get; set; } private object selectGame;
/// <summary>
/// 当前选中项
/// </summary>
public object SelectGame
{
get { return selectGame; }
set
{
selectGame = value;
base.RaisePropertyChanged("SelectGame");
}
}
#endregion #region Methods
private void Init()
{
GameSourceList = new ObservableCollection<ItemModel>();
GroupSourceList = new ObservableCollection<GroupModel>();
GroupModel gp1 = new GroupModel();
#region 模拟数据
gp1.GroupName = "竞技游戏";
gp1.ItemModelList = new ObservableCollection<ItemModel>();
gp1.ItemModelList.Add(new ItemModel() { ItemName = "CS GO" });
gp1.ItemModelList.Add(new ItemModel() { ItemName = "星际争霸2" });
gp1.ItemModelList.Add(new ItemModel() { ItemName = "FIFA 14" });
gp1.GroupCount = gp1.ItemModelList.Count;
GroupModel gp2 = new GroupModel();
gp2.GroupName = "网络游戏";
gp2.ItemModelList = new ObservableCollection<ItemModel>();
gp2.ItemModelList.Add(new ItemModel() { ItemName = "CS OnLine" });
gp2.ItemModelList.Add(new ItemModel() { ItemName = "街头篮球" });
gp2.ItemModelList.Add(new ItemModel() { ItemName = "完美世界" });
gp2.GroupCount = gp2.ItemModelList.Count;
GroupModel gp3 = new GroupModel();
gp3.GroupName = "休闲游戏";
gp3.ItemModelList = new ObservableCollection<ItemModel>();
gp3.ItemModelList.Add(new ItemModel() { ItemName = "德州扑克" });
gp3.ItemModelList.Add(new ItemModel() { ItemName = "街头篮球" });
gp3.ItemModelList.Add(new ItemModel() { ItemName = "完美世界" });
GroupSourceList.Add(gp1);
GroupSourceList.Add(gp2);
GroupSourceList.Add(gp3);
gp3.GroupCount = gp3.ItemModelList.Count;
#endregion
DragEnterCommand = new RelayCommand<DragEventArgs>(DragEnter);
DropCommand = new RelayCommand<DragEventArgs>(Drop);
} private void DragEnter(DragEventArgs args)
{
if (SelectGame.GetType() == typeof(ItemModel)) //如果拖拽的对象是"游戏"则接受之
{
args.Effects = DragDropEffects.Move;
System.Console.WriteLine("accept");
}
else
{
args.Effects = DragDropEffects.None; //否则拒绝接受拖拽
System.Console.WriteLine("no accept");
}
args.Handled = true;
} private void Drop(DragEventArgs args)
{
GameSourceList.Add(SelectGame as ItemModel); //将接受到的"游戏"写入ListBox
}
#endregion #region Commands public ICommand DragEnterCommand { get; set; } public ICommand DropCommand { get; set; }
#endregion
}

到这里一个简单的拖拽就完成了。
原地址:http://www.cnblogs.com/ShenNan/p/4275494.html
MVVM模式下实现拖拽的更多相关文章
- MVVM模式下弹出窗体
原地址:http://www.cnblogs.com/yk250/p/5773425.html 在mvvm模式下弹出窗体,有使用接口模式传入参数new一个对象的,还有的是继承于一个window,然后在 ...
- Silverlight中在MVVM模式下对DatagridRow选择控件封装
在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...
- js架构设计模式——MVVM模式下,ViewModel和View,Model有什么区别
MVVM模式下,ViewModel和View,Model有什么区别 Model:很简单,就是业务逻辑相关的数据对象,通常从数据库映射而来,我们可以说是与数据库对应的model. View:也很简单,就 ...
- MVVM模式下 DataTemplate 中控件的绑定
今天给ListBox中通过DataTemplate生成的Button绑定命令时,一开始Button始终找不到绑定的命令.现找到了正确的绑定方式,特来记录一下. 先上个正确的示例: <ListBo ...
- WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题
问题:WPF中MVVM模式下 ListView绑定ListCollectionView时,CurrentChanged无法触发 解决方案: 初期方案:利用ListView的SelectionChang ...
- WPF实战案例-MVVM模式下在Xaml中弹出窗体
相信很多学习和开发wpf项目的同学都了解过mvvm模式,同样,在mvvm模式下会有一个不可忽视的问题,就是怎么在xaml中弹出窗体,而不破坏MVVM本身的结构. 关于弹出窗体的方式还是很多的,本文先讲 ...
- 学习笔记---Javascript事件Event、IE浏览器下的拖拽效果
学习笔记---Javascript事件Event.IE浏览器下的拖拽效果 1. 关于event常用属性有returnValue(是否允许事件处理继续进行, false为停止继续操作).srcE ...
- WPF MVVM模式下ComboBox级联效果 选择第一项
MVVM模式下做的省市区的级联效果.通过改变ComboBox执行命令改变市,区. 解决主要问题就是默认选中第一项 1.首先要定义一个属性,继承自INotifyPropertyChanged接口.我这里 ...
- MVVM模式下 修改 store的ajax请求url。
MVVM模式下 修改 store的ajax请求url. view.down('Pro').getViewModel().getStore('xx_store').proxy.url = "s ...
随机推荐
- 2076 Problem F Quick Brown Fox
题目描述 A pangram is a phrase that includes at least one occurrence of each of the 26 letters, ‘a’. . . ...
- c++从文件中读取特定字符串问题的总结
1.每次从文件中读出一行作为一个字符串 可以用ifstream()函数来打开一个文件,然后用while加getline()函数即可每次读一行文件,直到文件结束 #include<unistd.h ...
- docker基础命令
docker version ...
- 【Python升级录】--基础知识
创建角色成功! 正在载入python........ [python介绍] python是一门动态解释性的强类型定义语言. python的创始人为吉多·范罗苏姆(Guido van Rossum).1 ...
- IOS- 数据存储
在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...
- Qt 添加启动项
QString application_name = QApplication::applicationName(); QSettings *settings = new QSettings(&quo ...
- CUDA中并行规约(Parallel Reduction)的优化
转自: http://hackecho.com/2013/04/cuda-parallel-reduction/ Parallel Reduction是NVIDIA-CUDA自带的例子,也几乎是所有C ...
- RedHat下安装MySQL
下载mysql 解压tar -xvf mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar 安装MySQL-server包 rpm -ivh mysql-community ...
- wireshark_Couldn’t run /usr/sbin/dumpcap in child process: Permission denied
关于Wireshark出现:Couldn't run /usr/sbin/dumpcap in child process: Permission denied Are you a member of ...
- DB2应用中嵌入式SQL取值入本地变量
Declare section for host variables in C and C++ embedded SQL applications You must use an SQL declar ...