Grid上拖动控件:
<UserControl x:Class="SilverlightApplication2.SilverlightControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle Height="147" HorizontalAlignment="Left" Margin="122,72,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="158" MouseLeftButtonDown="rectangle1_MouseLeftButtonDown" MouseLeftButtonUp="rectangle1_MouseLeftButtonUp" MouseMove="rectangle1_MouseMove">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF638D1F" Offset="0" />
<GradientStop Color="#FF5EA785" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<sdk:Label Height="20" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Width="94" Content="Grid上拖动控件" />
</Grid>
</UserControl>
public partial class SilverlightControl1 : UserControl
{
bool isDragDropInEffect = false;
Point pos = new Point();
public SilverlightControl1()
{
InitializeComponent();
}
private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement fEle = sender as FrameworkElement;
isDragDropInEffect = true;
pos = e.GetPosition(null);
fEle.CaptureMouse();
fEle.Cursor = Cursors.Hand;
}
private void rectangle1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragDropInEffect)
{
FrameworkElement ele = sender as FrameworkElement;
isDragDropInEffect = false;
ele.ReleaseMouseCapture();
}
}
private void rectangle1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragDropInEffect)
{
FrameworkElement currEle = sender as FrameworkElement;
double xPos = e.GetPosition(null).X - pos.X + currEle.Margin.Left;
double yPos = e.GetPosition(null).Y - pos.Y + currEle.Margin.Top;
currEle.Margin = new Thickness(xPos,yPos, 0, 0);
pos = e.GetPosition(null);
}
}
}
Canvas上拖动控件:
<UserControl x:Class="SilverlightApplication2.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:my1="clr-namespace:SilverlightApplication2" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Canvas x:Name="LayoutRoot" Background="White">
<Rectangle Height="76" HorizontalAlignment="Left" Margin="68,37,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="119" Fill="#FFAA3636" Canvas.Left="0" Canvas.Top="0"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="155,145,0,0" Name="button1" VerticalAlignment="Top" Width="75" ClickMode="Hover" />
<sdk:Label Canvas.Left="0" Canvas.Top="0" Height="28" Name="label1" Width="120" Content="Canvas上拖动控件" />
</Canvas>
</UserControl>
public partial class MainPage : UserControl
{
bool isDragDropInEffect = false;
Point pos=new Point();
public MainPage()
{
InitializeComponent();
foreach (UIElement uiEle in LayoutRoot.Children)
{
uiEle.MouseMove += new MouseEventHandler(Element_MouseMove);
uiEle.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
uiEle.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
}
}
void Element_MouseMove(object sender, MouseEventArgs e)
{
if (isDragDropInEffect)
{
FrameworkElement currEle = sender as FrameworkElement;
double xPos = e.GetPosition(null).X - pos.X + (double)currEle.GetValue(Canvas.LeftProperty);
double yPos = e.GetPosition(null).Y - pos.Y + (double)currEle.GetValue(Canvas.TopProperty);
currEle.SetValue(Canvas.LeftProperty, xPos);
currEle.SetValue(Canvas.TopProperty, yPos );
pos = e.GetPosition(null);
}
}
void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement fEle = sender as FrameworkElement;
isDragDropInEffect = true;
pos = e.GetPosition(null);
fEle.CaptureMouse();
fEle.Cursor = Cursors.Hand;
}
void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragDropInEffect)
{
FrameworkElement ele = sender as FrameworkElement;
isDragDropInEffect = false;
ele.ReleaseMouseCapture();
}
}
}