开发者

How to make PanelDragDropTarget act only as a target and not as a source?

开发者 https://www.devze.com 2023-03-19 08:07 出处:网络
I created a new solution. I added a link to System.Windows.Controls.Toolkit to my Silverlight project and wrote this code:

I created a new solution. I added a link to System.Windows.Controls.Toolkit to my Silverlight project and wrote this code:

XAML:

<UserControl x:Class="SilverlightApplication4.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"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid>
        <toolkit:PanelDragDropTarget Margin="0,0,150,150" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" AllowedSourceEffects="Copy">
            <Grid Name="grid1" Background="Blue">
                    <Rectangle Height="40" HorizontalAlignment="Left" M开发者_StackOverflowargin="5,5,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="80" Fill="Red" />
                </Grid>
            </toolkit:PanelDragDropTarget>
        <toolkit:PanelDragDropTarget VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="150,150,0,0" AllowDrop="True" Drop="PanelDragDropTarget_Drop" AllowedSourceEffects="None">
            <Grid Name="grid2" Background="Green" />
        </toolkit:PanelDragDropTarget>
        </Grid>
    </Grid>
</UserControl>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication4
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void PanelDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
        {
            Rectangle myRectangle = new Rectangle() { Margin = new Thickness(5,5,0,0), Height = 40, Width = 80, 
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Top,
                        StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.Red)};
            grid2.Children.Add(myRectangle);
        }
    }
}

Now when I drag and drop the small red rectangle from grid1 onto grid2 everything works fine. But when I touch the new added rectangle in grid2 it shows visible signs that it can be dragged. My question is how to make a second PanelDragDropTarget (with grid2 inside) to act only as a target for drag and drop and not as a source? I mean how to block the possibility for a user to drag the new created rectangle in grid2, i.e. to exclude any visible signs that this new rectangle is draggable? Because it's not supposed to be draggable in my case.


I found a solution. For the PanelDragDropTarget adorner of the grid2 I defined an event handler for its ItemDragStarting event.

    private void PanelDragDropTarget_ItemDragStarting(object sender, ItemDragEventArgs e)
    {
        e.Cancel = true;
        e.Handled = true;
    }

Now when I try to drag elements in grid2 nothing happens (that was my purpose).


Have you tried AllowedSourceEffects="None", works for me in SL5...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号