如何使PanelDragDropTarget仅作为目标而不作为源?

时间:2011-07-12 03:32:32

标签: silverlight drag-and-drop panel

我创建了一个新的解决方案。我添加了一个指向System.Windows.Controls.Toolkit的链接到我的Silverlight项目并编写了这段代码:

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" Margin="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);
        }
    }
}

现在,当我将grid1中的小红色矩形拖放到grid2上时,一切正常。但是当我在grid2中触摸新添加的矩形时,它会显示可以拖动的可见迹象。我的问题是如何制作第二个PanelDragDropTarget(内置grid2)仅作为拖放目标而不是作为源?我的意思是如何阻止用户在grid2中拖动新创建的矩形的可能性,即排除这个新矩形可拖动的任何可见迹象?因为在我的情况下它不应该是可拖动的。

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。对于grid2的PanelDragDropTarget装饰器,我为其ItemDragStarting事件定义了一个事件处理程序。

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

现在,当我尝试在grid2中拖动元素时,没有任何事情发生(这是我的目的)。

答案 1 :(得分:1)

您是否尝试过AllowedSourceEffects =“None”,在SL5中适合我...