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 ...
随机推荐
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- CString之GetBuffer与ReleaseBuffer
我们知道,CString是MFC中提供的方便字符串操作的一个类,非常好使,具有自动动态内存管理功能. GetBuffer()主要作用是将字符串的缓冲区长度锁定: ReleaseBuffer()则是解除 ...
- 【leetcode】Restore IP Addresses (middle)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- iOS push与present Controller的区别
push与present都可以推出新的界面.present与dismiss对应,push和pop对应.present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他 ...
- struts2域值操作
1.通过servletActionContext类 /*** * 获得方式一:通过ServletActionContext类 * 提供的静态方法获得原始的web对象,直接和servlet的API耦合 ...
- asp.net 短信群发
protected void Btn_Save_Click(object sender, EventArgs e) { string Contents = this.Txt_SmsContents.T ...
- 存储过程使用CTE 和 case when
未用SQL CTE and case when: ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary1] )='ALL', )='ALL', )='AL ...
- August 19th 2016 Week 34th Friday
Friends are not the people you meet at the top, they are the people who were with you at the bottom. ...
- ui组件--弹出层layer的使用
帮助文档 下载地址 演示地址
- 模拟赛1102d1
炮(cannon)[题目描述]众所周知,双炮叠叠将是中国象棋中很厉害的一招必杀技.炮吃子时必须隔一个棋子跳吃,即俗称"炮打隔子". 炮跟炮显然不能在一起打起来,于是rly一天借来了 ...