2011-07-12 51 views
2

我創建了一個新的解決方案。我添加了一個鏈接到System.Windows.Controls.Toolkit到我的Silverlight項目,並寫了這個代碼:如何使PanelDragDropTarget僅充當目標而不是源?

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中拖動新創建的矩形的可能性,即排除這個新矩形可拖動的任何可見跡象?因爲在我的情況下它不應該是可拖動的。

回答

1

我找到了解決方案。對於Grid2的PanelDragDropTarget裝飾器,我爲其ItemDragStarting事件定義了一個事件處理程序。

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

現在,當我嘗試在grid2中拖動元素時,沒有任何反應(這是我的目的)。

1

您是否嘗試過AllowedSourceEffects =「無」,適用於SL5中的我...

相關問題