silverlight 行为浅谈---datagrid数据行上移下移
闲来无事,突然想到datagrid控件,虽然列能够拖动,但是行能不能拖动呢? 上网查了点资料,让人郁闷的是,没有相关例子,于是狠心自己花了点时间写了一个简单的,虽然不能像列那样拖动,但是基本上也能实现行的上移下移了,废话少说,先看图:
思路:
一:加载数据:
由于比较简单,所以写了个简单的数据提供程序:
public class Student
{
public int id { get; set; }
public string name { get; set; }
}
有一点我这里需要声明一下:我写的这个操作(Action)是附加在按钮上边的,以便操作数据的上移下移。
声明两个泛型列表,做数据源使用
private List<Student> data1 = new List<Student>();
private List<Student> data2 = new List<Student>();
接下来就是加载数据了
protected override void OnAttached()
{
base.OnAttached();
(this.AssociatedObject as Button ).Loaded += new RoutedEventHandler(Action1_Loaded);
(this.AssociatedObject as Button).Click += new RoutedEventHandler(Action1_Click);
}
void Action1_Loaded(object sender, RoutedEventArgs e)
{
data1.Clear();
init();
(this.TargetSource as DataGrid).ItemsSource = data1;
}
public List <Student > init()
{
for (int a = 1; a < 10; a++)
{
Student stu = new Student();
stu.id = a;
stu.name = "姓名" + a.ToString();
data1.Add(stu );
}
return data1;
}
当然这里的 TargetSource 是我们要操作的datagrid,当然是同过 依赖属性或附加属性 传过来的,哪来看一下依赖属性
#region 依赖属性
/// <summary>
/// 所附加的目标datagrid
/// </summary>
public static readonly DependencyProperty TargetSourceProperty =
DependencyProperty.Register("TargetSource", typeof(System.Windows.Controls.DataGrid), typeof(Action1), null);
public DataGrid TargetSource
{
get { return (DataGrid)GetValue(Action1.TargetSourceProperty); }
set { SetValue(Action1.TargetSourceProperty, value); }
}
/// <summary>
/// 0下移 1上移
/// </summary>
public static readonly DependencyProperty TargetTypeProperty =
DependencyProperty.Register("TargetType", typeof(int ), typeof(Action1),new PropertyMetadata (0) );
public int TargetType
{
get { return (int)GetValue(Action1.TargetTypeProperty); }
set { SetValue(Action1.TargetTypeProperty, value); }
}
#endregion
因为比较简单,所以这里小弟就不多说了。
二:接下来肯定是点击按钮 进行上移下移的操作了
先来看下xaml :
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:Actions="clr-namespace:Actions;assembly=Actions" x:Class="SilverlightApplication1.MainPage"
mc:Ignorable="d"
d:DesignHeight="413" d:DesignWidth="654">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Height="258" HorizontalAlignment="Left" Margin="116,61,0,0" x:Name="dataGrid1" VerticalAlignment="Top" Width="463" SelectedIndex="0" />
<Button Content="上移" Height="23" HorizontalAlignment="Left" Margin="116,354,0,0" Name="button1" VerticalAlignment="Top" Width="61" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Actions:Action1 TargetObject="{Binding ElementName=dataGrid1}" TargetSource="{Binding ElementName=dataGrid1}" TargetType="1"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="下移" Height="23" HorizontalAlignment="Left" Margin="215,354,0,0" Name="button2" VerticalAlignment="Top" Width="75" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Actions:Action1 TargetSource="{Binding ElementName=dataGrid1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</UserControl>
接下来肯定是行为当中的操作了
void Action1_Click(object sender, RoutedEventArgs e)
{
DataGrid dg = this.TargetSource as DataGrid;
index = dg.SelectedIndex;
if (dg.SelectedIndex == -1)
{
MessageBox.Show("没有选择");
}
else
{
if (dg.SelectedIndex < data1.Count - 1 )
{
//下移
if (this.TargetType == 0)
{
Student stu = data1[dg.SelectedIndex + 1];
data1[dg.SelectedIndex + 1] = data1[dg.SelectedIndex];
data1[dg.SelectedIndex] = stu;
dg.ItemsSource = updatedata(data1 );
index = index + 1;
dg.SelectedIndex = index;
}
else
{
//上移
if (dg.SelectedIndex != 0)
{
Student stu1 = data1[dg.SelectedIndex - 1];
data1[dg.SelectedIndex - 1] = data1[dg.SelectedIndex];
data1[dg.SelectedIndex] = stu1;
dg.ItemsSource = updatedata(data1);
index = index - 1;
dg.SelectedIndex = index;
}
}
}
}
}
List <Student> updatedata(List <Student > list)
{
data2.Clear();
for (int a = 0; a < list.Count; a++)
{
data2.Add(list[a]);
}
return data2 ;
}
整体写下来,总是感觉updatedata 这个方法又点多余,又兴趣的朋友 可以自己去掉,原谅小弟懒惰,这里就不去了,哈哈.......................
整体没又难度,就算是我写博客练手把
代码:
xaml :
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:Actions="clr-namespace:Actions;assembly=Actions" x:Class="SilverlightApplication1.MainPage"
mc:Ignorable="d"
d:DesignHeight="413" d:DesignWidth="654">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Height="258" HorizontalAlignment="Left" Margin="116,61,0,0" x:Name="dataGrid1" VerticalAlignment="Top" Width="463" SelectedIndex="0" />
<Button Content="上移" Height="23" HorizontalAlignment="Left" Margin="116,354,0,0" Name="button1" VerticalAlignment="Top" Width="61" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Actions:Action1 TargetObject="{Binding ElementName=dataGrid1}" TargetSource="{Binding ElementName=dataGrid1}" TargetType="1"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="下移" Height="23" HorizontalAlignment="Left" Margin="215,354,0,0" Name="button2" VerticalAlignment="Top" Width="75" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Actions:Action1 TargetSource="{Binding ElementName=dataGrid1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</UserControl>
行为代码:
using System;
using System.Collections.Generic;
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;
using System.Windows.Interactivity;
namespace Actions
{
//
// 如果想针对元素而不是其父项来执行您的 Action,请
// 从 TargetedTriggerAction 中展开类,而不是从 TriggerAction 中展开类
//
public class Action1 : TargetedTriggerAction <UIElement>
{
#region 依赖属性
/// <summary>
/// 所附加的目标datagrid
/// </summary>
public static readonly DependencyProperty TargetSourceProperty =
DependencyProperty.Register("TargetSource", typeof(System.Windows.Controls.DataGrid), typeof(Action1), null);
public DataGrid TargetSource
{
get { return (DataGrid)GetValue(Action1.TargetSourceProperty); }
set { SetValue(Action1.TargetSourceProperty, value); }
}
/// <summary>
/// 0下移 1上移
/// </summary>
public static readonly DependencyProperty TargetTypeProperty =
DependencyProperty.Register("TargetType", typeof(int ), typeof(Action1),new PropertyMetadata (0) );
public int TargetType
{
get { return (int)GetValue(Action1.TargetTypeProperty); }
set { SetValue(Action1.TargetTypeProperty, value); }
}
#endregion
private List<Student> data1 = new List<Student>();
private List<Student> data2 = new List<Student>();
private int index = 0;//选中行的索引
public Action1()
{
// 在此点下面插入创建对象所需的代码。
}
protected override void Invoke(object o)
{
// 插入代码以定义在触发/调用时 Action 将执行的操作。
}
protected override void OnAttached()
{
base.OnAttached();
(this.AssociatedObject as Button ).Loaded += new RoutedEventHandler(Action1_Loaded);
(this.AssociatedObject as Button).Click += new RoutedEventHandler(Action1_Click);
}
void Action1_Click(object sender, RoutedEventArgs e)
{
DataGrid dg = this.TargetSource as DataGrid;
index = dg.SelectedIndex;
if (dg.SelectedIndex == -1)
{
MessageBox.Show("没有选择");
}
else
{
if (dg.SelectedIndex < data1.Count - 1 )
{
//下移
if (this.TargetType == 0)
{
Student stu = data1[dg.SelectedIndex + 1];
data1[dg.SelectedIndex + 1] = data1[dg.SelectedIndex];
data1[dg.SelectedIndex] = stu;
dg.ItemsSource = updatedata(data1 );
index = index + 1;
dg.SelectedIndex = index;
}
else
{
//上移
if (dg.SelectedIndex != 0)
{
Student stu1 = data1[dg.SelectedIndex - 1];
data1[dg.SelectedIndex - 1] = data1[dg.SelectedIndex];
data1[dg.SelectedIndex] = stu1;
dg.ItemsSource = updatedata(data1);
index = index - 1;
dg.SelectedIndex = index;
}
}
}
}
}
List <Student> updatedata(List <Student > list)
{
data2.Clear();
for (int a = 0; a < list.Count; a++)
{
data2.Add(list[a]);
}
return data2 ;
}
void Action1_Loaded(object sender, RoutedEventArgs e)
{
data1.Clear();
init();
(this.TargetSource as DataGrid).ItemsSource = data1;
}
/// <summary>
/// 绑定数据源
/// </summary>
public List <Student > init()
{
for (int a = 1; a < 10; a++)
{
Student stu = new Student();
stu.id = a;
stu.name = "姓名" + a.ToString();
data1.Add(stu );
}
return data1;
}
}
public class Student
{
public int id { get; set; }
public string name { get; set; }
}
}
另外注意一点:如果行为当中用到datagrid 命名空间,请引用
System.Windows.Controls.Data