Tags: | Categories: Silverlight Posted by Christoph on 3/3/2010 11:51 AM | Comments (0)

Ich habe in einem Projekt das Problem, dass Elemente per Drag and Drop von einer Liste in eine andere Liste verschoben werden sollen, ich aber in der Zielliste nicht alle Quellelemente erlaiben will.

Das könnte im XAML also so aussehen:

<UserControl
	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:msWindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
	xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
	x:Class="DragAndDrop.MainPage"
	mc:Ignorable="d" d:DesignWidth="467" 
	d:DesignHeight="225" Background="#FFDEDEDE" 
	BorderBrush="#FFEA4747" BorderThickness="1" FontSize="18"
	>

<Grid x:Name="LayoutRoot" Background="White">
	<Grid.ColumnDefinitions>
		<ColumnDefinition Width="*"/>
		<ColumnDefinition Width="*"/>
	</Grid.ColumnDefinitions>
	
	<controlsToolkit:ListBoxDragDropTarget 
		HorizontalContentAlignment="Stretch" 
		VerticalContentAlignment="Stretch"
		x:Name="DragSource">
		<ListBox x:Name="lbSource" DisplayMemberPath="Name" />
	</controlsToolkit:ListBoxDragDropTarget>
	
	<controlsToolkit:ListBoxDragDropTarget 
		Grid.Column="1" 
		HorizontalContentAlignment="Stretch" 
		VerticalContentAlignment="Stretch"
		x:Name="DragDest"
		msWindows:DragDrop.AllowDrop="true">
		<ListBox x:Name="lbDest" DisplayMemberPath="Name" />
	</controlsToolkit:ListBoxDragDropTarget>
	
</Grid>
</UserControl>

und hier ist der passende Codebehind für den DragOver event, der die Elemente prüft:

Imports Microsoft.Windows
Imports System.Collections.ObjectModel

Partial Public Class MainPage

	Inherits UserControl

	Public Sub New()

		InitializeComponent()

		Dim Persons As New List(Of Person)

		Persons.Add(New Person With {.Name = "Hans Meiser", .IsAllowed = False})
		Persons.Add(New Person With {.Name = "Günther Jauch", .IsAllowed = False})
		Persons.Add(New Person With {.Name = "James Brown", .IsAllowed = True})
		Persons.Add(New Person With {.Name = "Elvis", .IsAllowed = True})
		Persons.Add(New Person With {.Name = "Marianne und Michael", .IsAllowed = False})

		lbSource.ItemsSource = Persons

		DragDest.AllowedSourceEffects = DragDropEffects.Copy

	End Sub


	Private Sub DragDest_DragOver(ByVal sender As Object, _
			 ByVal e As Microsoft.Windows.DragEventArgs) _
			 Handles DragDest.DragOver

		'--ignore Sort Events
		If e.Effects = 3 Then Return

		'--Fetch Selection
		Dim Args As ItemDragEventArgs = e.Data.GetData(e.Data.GetFormats()(0))
		Dim Sel As SelectionCollection = Args.Data

		'--Build Persons from Selection
		Dim Persons = (From Pe In Sel Select DirectCast(Pe.Item, Person)).ToList

		'--Handle Drag Event
		If (From P In Persons Where Not P.IsAllowed).Count > 0 Then
			e.Handled = True
			e.Effects = DragDropEffects.None
		End If

	End Sub

End Class

Mehr dazu gibt’s bei stackoverflow.com oder direkt hier

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading