如何创建可拖动的wpf堆栈面板? [英] how to create draggable wpf stack panel?
问题描述
我想在创建可拖动的堆栈面板时获得一些帮助。当选择组合框项目时,我必须自动填充图像和几个包含图像和标签的可拖动堆栈面板。应将这些堆叠面板放在图像上。
任何人都可以帮助我。我尝试了很多方法,从来没有真正奏效过。
提前致谢
编辑:
您好db7uk,
感谢您的回答,但我已尝试过所有这些链接。它们是关于拖动listview项目。我想在图像上拖放一个堆栈面板。实际上,会有几个堆栈面板,我想拖放图像。
private void CmbFloorLevel_SelectionChanged( object sender,SelectionChangedEventArgs e)
{
string rootDic = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @ \Images\UploadFiles);
string [] selecteditem = CmbFloorLevel.SelectedItem.ToString()。Split(' '跨度>)。
roomsList = fpDB.getRooms(selecteditem [ 0 ]。ToString()。Trim());
// rmIcon = new RoomIcon [roomsList.Count];
string filename = getFloorplan();
if (filename!= string .Empty)
{
ImgFloorPlan.Source = new BitmapImage( new Uri(rootDic + \\ + filename));
}
else
{
ImgFloorPlan.Source = new BitmapImage( new Uri(System.IO.Path.GetFullPath( 图片/ blank.png跨度>)));
}
ImgFloorPlan.BringIntoView();
loadRooms();
}
public void loadRooms()
{
// 删除所有以前的画布标签
int i;
for ( int index = CnvsFloorPlan.Children.Count - 1 ; index > = 0 ; index--)
{
if (CnvsFloorPlan.Children [index] 标签)
{
CnvsFloorPlan.Children.RemoveAt(index);
}
}
for ( int index = GrdFloorPlan.Children.Count - 1 ; index > = 0 ; index--)
{
if (GrdFloorPlan.Children [index] 是标签)
GrdFloorPlan.Children.RemoveAt(index);
}
i = 0 ;
StkRooms = new StackPanel();
StkRooms.Name = StkRooms + i.ToString();
StkRooms.Background = Brushes.Gray;
StkRooms.Margin = new 厚度( 2 );
StkRooms.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
foreach ( string room in roomsList)
{
lbl = new Label();
lbl.Content = room;
lbl.Foreground = Brushes.White;
lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
lbl.Background = Brushes.Gray;
lbl.Margin = new 厚度( 2 );
lbl.PreviewMouseDown + = new MouseButtonEventHandler(StkRooms_MouseDown);
lbl.PreviewMouseMove + = new MouseEventHandler(StkRooms_MouseMove);
lbl.Cursor = Cursors.Hand;
lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
StkRooms.Children.Add(lbl);
i ++;
}
CnvsFloorPlan.Children.Add(StkRooms);
}
private void StkRooms_MouseDown( object sender,MouseButtonEventArgs e)
{
dragStarted = true ;
if (e.Source == lbl)
{
DragDrop.DoDragDrop(lbl,lbl,DragDropEffects.Move);
}
base .OnPreviewMouseDown(e);
}
private void StkRooms_MouseMove( object sender,MouseEventArgs e)
{
if (dragStarted&& sender 标签)
{
}
}
private void ImgFloorPlan_Drop( object sender,DragEventArgs e)
{
标签l = e.Data.GetData( typeof (标签)) as 标签;
// l.PreviewMouseDown + = new MouseButtonEventHandler(StkRooms_MouseDown);
< span class =code-comment> // l.Cursor = Cursors.Hand;
// l.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
l.Background = Brushes.Gray;
l.Height = 25 ;
Window wnd = Window.GetWindow( this );
Point mousepos = Mouse.GetPosition(wnd);
// MessageBox.Show(mousepos.X.ToString()+,+ mousepos.Y。 ToString());
l.Margin = new 厚度{Left = mousepos.X,Bottom = mousepos.Y};
面板元素= l.Parent as 面板;
element.Children.Remove(l);
// CnvsFloorPlan.Children.Remove(l);
GrdFloorPlan.Children 。新增(升);
}
无论如何,每当我填充堆叠面板时,图像都不会显示。当我尝试将堆栈面板放在特定位置时,它会被丢弃在不同的位置。我不知道我做错了什么。
任何人都可以帮助我。
提前致谢
一些优秀的拖放示例。
不是100%确定您的初始要求是什么。你能用一些尝试过的东西改善答案吗?
在WPF中拖放 [ ^ ]
拖动和WPF中的内容 - 第二部分 [ ^ ]
http://code.google.com / p / gong-wpf-dragdrop / [ ^ ]
拖放WPF ListView中的项目 [ ^ ]
有谁能帮我这个代码?我正在追赶时间。
谢谢
使用Thumb可以轻松地拖放到wpf中,如下所述:
如何在代码隐藏的wpf中将多个标签拖放到图像上?
Hi,
I would like to get some help in creating draggable stack panel. When a combobox item is selected, I have to automatically populate an image and several draggable stack panels which contain an image and a label. These stack panels should be dropped over the image.
Can anyone please help me with this. I have tried a lot of ways, which never really worked.
Thanks in advance
EDIT:
Hi db7uk,
Thank you for the answer, but I have tried all those links. They are about dragging listview items. I want to drag and drop a stackpanel over an image. In fact, there will be several stack panels, which I want to drag and drop over the image.
private void CmbFloorLevel_SelectionChanged(object sender, SelectionChangedEventArgs e) { string rootDic = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"\Images\UploadFiles"); string[] selecteditem = CmbFloorLevel.SelectedItem.ToString().Split('.'); roomsList = fpDB.getRooms(selecteditem[0].ToString().Trim()); //rmIcon = new RoomIcon[roomsList.Count]; string filename = getFloorplan(); if (filename != string.Empty) { ImgFloorPlan.Source = new BitmapImage(new Uri(rootDic + "\\" + filename)); } else { ImgFloorPlan.Source = new BitmapImage(new Uri(System.IO.Path.GetFullPath("Images/blank.png"))); } ImgFloorPlan.BringIntoView(); loadRooms(); } public void loadRooms() { //Removing all previous canvas labels int i; for (int index = CnvsFloorPlan.Children.Count - 1; index >= 0; index--) { if (CnvsFloorPlan.Children[index] is Label) { CnvsFloorPlan.Children.RemoveAt(index); } } for (int index = GrdFloorPlan.Children.Count - 1; index >= 0; index--) { if (GrdFloorPlan.Children[index] is Label) GrdFloorPlan.Children.RemoveAt(index); } i = 0; StkRooms = new StackPanel(); StkRooms.Name = "StkRooms" + i.ToString(); StkRooms.Background = Brushes.Gray; StkRooms.Margin = new Thickness(2); StkRooms.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; foreach (string room in roomsList) { lbl = new Label(); lbl.Content = room; lbl.Foreground = Brushes.White; lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; lbl.Background = Brushes.Gray; lbl.Margin = new Thickness(2); lbl.PreviewMouseDown += new MouseButtonEventHandler(StkRooms_MouseDown); lbl.PreviewMouseMove += new MouseEventHandler(StkRooms_MouseMove); lbl.Cursor = Cursors.Hand; lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; StkRooms.Children.Add(lbl); i++; } CnvsFloorPlan.Children.Add(StkRooms); } private void StkRooms_MouseDown(object sender, MouseButtonEventArgs e) { dragStarted = true; if (e.Source == lbl) { DragDrop.DoDragDrop(lbl, lbl, DragDropEffects.Move); } base.OnPreviewMouseDown(e); } private void StkRooms_MouseMove(object sender, MouseEventArgs e) { if (dragStarted && sender is Label) { } } private void ImgFloorPlan_Drop(object sender, DragEventArgs e) { Label l = e.Data.GetData(typeof(Label)) as Label; //l.PreviewMouseDown += new MouseButtonEventHandler(StkRooms_MouseDown); //l.Cursor = Cursors.Hand; //l.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; l.Background = Brushes.Gray; l.Height = 25; Window wnd = Window.GetWindow(this); Point mousepos = Mouse.GetPosition(wnd); //MessageBox.Show(mousepos.X.ToString() + "," + mousepos.Y.ToString()); l.Margin = new Thickness { Left = mousepos.X, Bottom = mousepos.Y }; Panel element = l.Parent as Panel; element.Children.Remove(l); //CnvsFloorPlan.Children.Remove(l); GrdFloorPlan.Children.Add(l); }
Somehow whenever i populate the stackpanels, the image doesn''t show. And when i try to drop the stackpanel at a particular location, it gets dropped at a different location. I do not know what I am doing wrong.
Can anyone please help me with this.
Thanks in advance解决方案Some excellent drag drop examples.
Not 100 % sure what your initial requirement is though. Could you improve answer with some things you have tried?
Drag and Drop in WPF[^]
Drag and Drop in WPF - Part II[^]
http://code.google.com/p/gong-wpf-dragdrop/[^]
Drag and Drop Items in a WPF ListView[^]
Can anyone please help me with this code? I am running behind time.
Thanks
Drag and drop in wpf can be done easily using Thumbs as explained here:
how to drag and drop multiple labels over image in wpf from codebehind?
这篇关于如何创建可拖动的wpf堆栈面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!