Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Drag and Drop ListBox in Silverlight Child Windows
3 replies. Latest Post by quintelab on May 18, 2009.
(0)
quintelab
Member
1 points
2 Posts
05-13-2009 4:57 PM |
Drag and Drop does not work in Silverlight Child Windows? My english is bad. Sorry
Mog Lian...
All-Star
15914 points
1,551 Posts
05-18-2009 3:39 AM |
As I understand , you want to drag and drop controls contained in Childwindow. If I misunderstand you, please correct me.
I think it's quite the same with D&D in UserControl.
it's my sample
<controls:ChildWindow x:Class="SL3_test1.ChildWindow3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Width="400" Height="300" Title="ChildWindow3"> <Grid x:Name="LayoutRoot" Margin="2"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Canvas Grid.Row="0" x:Name="canvas1"> <Border CornerRadius="5" Padding="5" Background="Azure" MouseLeftButtonDown="Border_MouseLeftButtonDown" MouseLeftButtonUp="Border_MouseLeftButtonUp" MouseMove="Border_MouseMove"> <ListBox ItemsSource="abcde"/> </Border> </Canvas> <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" /> <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" /> </Grid></controls:ChildWindow>
code:
public partial class ChildWindow3 : ChildWindow { public ChildWindow3() { InitializeComponent(); } private void OKButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; } bool _isdrag; Point _startpos; double _xpos, _ypos; private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _isdrag = true; _startpos = e.GetPosition(this); var ue1 = sender as UIElement; _xpos = Canvas.GetLeft(ue1); _ypos = Canvas.GetTop(ue1); ue1.CaptureMouse(); } private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { _isdrag = false; } private void Border_MouseMove(object sender, MouseEventArgs e) { var ue1 = sender as UIElement; Point curpoint= e.GetPosition(this); double xoffset = curpoint.X - _startpos.X; double yoffset = curpoint.Y - _startpos.Y; if (_isdrag) { Canvas.SetLeft(ue1, _xpos + xoffset); Canvas.SetTop(ue1, _ypos + yoffset); } } }
KevD
181 points
38 Posts
05-18-2009 6:35 AM |
Hello, I'm not exactly sure what you're looking for, but you might want to take a look at this if you're working with drag/drop in SL: http://silverlightdragdrop.codeplex.com This is an open source project of mine to easily allow for dragging and dropping; drag/drop of a listbox should be easy with this, and even drag/drop of items inside a listbox is possible (make sure you get the latest source code drop & read the discussion board for that last one, as a few helpful properties have been added for such scenarios). Hope this helps! :-)
05-18-2009 12:57 PM |
Actually your example worked.Trying to drop one to another and was not able to. I will try to adapt.Thanks!!