本文介绍了有些错误与拖放从列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个的ListBox
我要拖上一个矩形掉落物品。一切正常,当我刚刚的项目,而不是的ItemsSource
和的ItemTemplate
,但现在它从采集填补。有一次,我把它放到矩形我想另一个文本框
来表示从属性值的保护
XAML
<列表框
X:NAME =listhelmets
HEIGHT =214
WIDTH =248
的ItemsSource ={结合ListHelmets}
IsSynchronizedWithCurrentItem =真
Canvas.Left =211
Canvas.Top = 72
PreviewMouseDown =helmet_MouseDown1
的PreviewMouseLeftButtonDown =helmet_PreviewMouseLeftButtonDown
PreviewMouseMove =helmet_PreviewMouseMove>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的方向=横向>
<图像来源={绑定路径=图片}WIDTH =56HEIGHT =61/>
<文本框的文本={绑定路径=保护}WIDTH =30HEIGHT =20/>
< / StackPanel的>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
代码背后
私人无效helmet_MouseDown1(对象发件人,MouseButtonEventArgs E)
{
_startPoint = e.GetPosition(NULL);
}
私人无效helmet_PreviewMouseMove(对象发件人,MouseEventArgs E)
{
点mousePos结构= e.GetPosition(NULL);
矢量差异= _startPoint - mousePos结构;
如果(e.LeftButton == MouseButtonState.Pressed&放大器;&安培;
(Math.Abs(diff.X)GT; SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
变种的ListBox =发件人列表框作为;
VAR ListBoxItem的= listBox.SelectedItem;
数据对象dragData =新的DataObject(_dropIdentifier,ListBoxItem的);
DragDrop.DoDragDrop(列表框,dragData,DragDropEffects.Move);
}
}
私人无效armature_MouseDown(对象发件人,MouseButtonEventArgs E)
{
_startPoint = e.GetPosition(NULL);
}
私人无效armature_PreviewMouseMove(对象发件人,MouseEventArgs E)
{
点mousePos结构= e.GetPosition(NULL);
矢量差异= _startPoint - mousePos结构;
如果(e.LeftButton == MouseButtonState.Pressed&放大器;&安培;
(Math.Abs(diff.X)GT; SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
变种的ListBox =发件人列表框作为;
VAR ListBoxItem的= listBox.SelectedItem;
数据对象dragData =新的DataObject(_dropIdentifier,ListBoxItem的);
DragDrop.DoDragDrop(列表框,dragData,DragDropEffects.Move);
}
}
私人无效weapons_MouseDown(对象发件人,MouseButtonEventArgs E)
{
_startPoint = e.GetPosition(NULL);
}
私人无效weapons_PreviewMouseMove(对象发件人,MouseEventArgs E)
{
点mousePos结构= e.GetPosition(NULL);
矢量差异= _startPoint - mousePos结构;
如果(e.LeftButton == MouseButtonState.Pressed&放大器;&安培;
(Math.Abs(diff.X)GT; SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
变种的ListBox =发件人列表框作为;
VAR ListBoxItem的= listBox.SelectedItem;
数据对象dragData =新的DataObject(_dropIdentifier,ListBoxItem的);
DragDrop.DoDragDrop(列表框,dragData,DragDropEffects.Move);
}
}
解决方案
一切没关系。我要让这个帖子,以防有人将尝试将它从列表框中
降
I have a ListBox
and I want to drag and drop Items on a rectangle. Everything was okay when I had just items instead of ItemsSource
and ItemTemplate
, but now it's filled from collection. Once I drop it on rectangle I want another TextBox
to show value from property "protection"
XAML
<ListBox
x:Name="listhelmets"
Height="214"
Width="248"
ItemsSource="{Binding ListHelmets}"
IsSynchronizedWithCurrentItem="True"
Canvas.Left="211"
Canvas.Top="72"
PreviewMouseDown="helmet_MouseDown1"
PreviewMouseLeftButtonDown="helmet_PreviewMouseLeftButtonDown"
PreviewMouseMove="helmet_PreviewMouseMove">
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=Image}" Width="56" Height="61"/>
<TextBox Text="{Binding Path=protection}" Width="30" Height="20" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
code behind
private void helmet_MouseDown1(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void helmet_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
private void armature_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void armature_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
private void weapons_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void weapons_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
解决方案
Everything is okay. I'll let this post in case someone will try to drag and drop from listbox
这篇关于有些错误与拖放从列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!