silverlight4中grid、canvas上拖放控件总结

本文介绍如何在Silverlight中实现Grid和Canvas上控件的拖动功能。通过监听鼠标按下、移动和释放事件,调整控件位置,实现平滑的拖动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
            }
        }
    }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值