重新排列在wrappanel中的CustomControl在wpf c# [英] Rearrange CustomControl inside wrappanel in wpf c#
问题描述
这是我的XAML代码
& DockPanel Grid.Column =1Margin =208,40,1,94Grid.Row =2
背景=白色AllowDrop =真>
< ScrollViewer AllowDrop =TrueVerticalScrollBarVisibility =AutoHorizontalScrollBarVisibility =HiddenWidth =443>
< StackPanel Width =443>
< WrapPanel x:Name =pnlHorizontalAlignment =Left
Height =Auto
VerticalAlignment =TopWidth =480 AllowDrop =True
/>
< / StackPanel>
< / ScrollViewer>
< / DockPanel>
我已经尝试将包装面板放在列表中,正如给定答案所建议的Ilan的答案),现在我的面板在代码背后是无法访问的。 AM我做错了什么?
这里我演示了按钮
,您可以根据需要进行修改。
Xaml
< WrapPanel x:Name =PnlBackground =YellowMargin =0,0,0,128Button.DragEnter =Button_DragEnterButton.MouseRightButtonDown =Button_MouseDown>
< Button Content =Btn1AllowDrop =True/>
< Button Content =Btn2AllowDrop =True/>
< Button Content =Btn3AllowDrop =True/>
< / WrapPanel>
代码
按钮btn_to_drag;
private void Button_MouseDown(object sender,MouseButtonEventArgs e)
{
btn_to_drag =(Button)e.Source;
DragDrop.DoDragDrop(btn_to_drag,btn_to_drag,DragDropEffects.Move);
}
private void Button_DragEnter(object sender,DragEventArgs e)
{
按钮btn =(Button)e.Source;
int where_to_drop = Pnl.Children.IndexOf(btn);
Pnl.Children.Remove(btn_to_drag);
Pnl.Children.Insert(where_to_drop,btn_to_drag);
}
另一种使用 DataObject
删除 btn_to_drag
声明的需要。
private void Button_MouseDown(object发件人,MouseButtonEventArgs e)
{
DataObject data = new DataObject(DataFormats.Serializable,(Button)e.Source);
DragDrop.DoDragDrop((DependencyObject)e.Source,data,DragDropEffects.Move);
}
private void Button_DragEnter(object sender,DragEventArgs e)
{
按钮btn_to_move =(Button)e.Data.GetData(DataFormats.Serializable);
int where_to_move = Pnl.Children.IndexOf((UIElement)e.Source);
int what_to_move = Pnl.Children.IndexOf(btn_to_move);
Pnl.Children.RemoveAt(what_to_move);
Pnl.Children.Insert(where_to_move,btn_to_move);
}
I am creating a customcontrol dynamically inside a wrappanel. Now i need the reorder the custom controls which are inside the wrappanel. Is it possible to rearrange the custom controls inside the wrappanel using drag and drop?
Here is my XAML code
<DockPanel Grid.Column="1" Margin="208,40,1,94" Grid.Row="2"
Background="White" AllowDrop="True">
<ScrollViewer AllowDrop="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" Width="443">
<StackPanel Width="443" >
<WrapPanel x:Name="pnl" HorizontalAlignment="Left"
Height="Auto"
VerticalAlignment="Top" Width="480" AllowDrop="True"
/>
</StackPanel>
</ScrollViewer>
</DockPanel>
I've tried put the wrap panel inside the list, as it was suggested by the given answer(Ilan's answer), and now my panel is not accessible in the code behind. AM i doing anything wrong?
Here I have demonstrated Buttons
, you can modify it for your needs.
Xaml
<WrapPanel x:Name="Pnl" Background="Yellow" Margin="0,0,0,128" Button.DragEnter="Button_DragEnter" Button.MouseRightButtonDown="Button_MouseDown">
<Button Content="Btn1" AllowDrop="True"/>
<Button Content="Btn2" AllowDrop="True"/>
<Button Content="Btn3" AllowDrop="True"/>
</WrapPanel>
Code
Button btn_to_drag;
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
btn_to_drag = (Button)e.Source;
DragDrop.DoDragDrop(btn_to_drag, btn_to_drag, DragDropEffects.Move);
}
private void Button_DragEnter(object sender, DragEventArgs e)
{
Button btn = (Button)e.Source;
int where_to_drop = Pnl.Children.IndexOf(btn);
Pnl.Children.Remove(btn_to_drag);
Pnl.Children.Insert(where_to_drop, btn_to_drag);
}
Another approach using DataObject
removing the need for btn_to_drag
declaration.
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
DataObject data = new DataObject(DataFormats.Serializable, (Button)e.Source );
DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Move);
}
private void Button_DragEnter(object sender, DragEventArgs e)
{
Button btn_to_move = (Button) e.Data.GetData(DataFormats.Serializable);
int where_to_move = Pnl.Children.IndexOf((UIElement)e.Source);
int what_to_move = Pnl.Children.IndexOf(btn_to_move);
Pnl.Children.RemoveAt(what_to_move);
Pnl.Children.Insert(where_to_move, btn_to_move);
}
这篇关于重新排列在wrappanel中的CustomControl在wpf c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!