1.ListBox在没有添加任何Item是的拖放可以完全参照Button的拖放
2.当ListBox添加了Item时,会出现ListBox不相应listBox1_MouseLeftButtonDown的问题
解决方法:
原因: ListBox中,MouseLeftButtonDown事件是一个RoutedEvent , 并且在routing过程中被标记为handled, 因此ListBox不会再抛出该事件,因此你的事件处理方法也无从相应。
解决: 使用UIElement.AddHandler 方法指定已经被handled的事件。
bool trackingMouseMove = false;
Point mousePosition;
public listbox添加项后的拖放()
{
InitializeComponent();
listBox1.AddHandler(ListBox.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(listBox1_MouseLeftButtonDown), true);
//listBox1.AddHandler(ListBox.MouseLeftButtonUpEvent,
// new MouseButtonEventHandler(listBox1_MouseLeftButtonUp), true);
//注意MouseMove不需要添加,只需要添加MouseLeftButtonDown即可
}
private void listBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
trackingMouseMove = true;
if (null != element)
{
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
}
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
double deltaV = e.GetPosition(null).Y - mousePosition.Y;
double deltaH = e.GetPosition(null).X - mousePosition.X;
double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty);
double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);
element.SetValue(Canvas.TopProperty, newTop);
element.SetValue(Canvas.LeftProperty, newLeft);
mousePosition = e.GetPosition(null);
}
}
3.===================拖放一个ListBox的Item到另外一个ListBox===============
主要代码
public partial class listbox拖放Item : Page
{
bool trackingMouseMove = false;
Point mousePosition;
double newTop;
double newLeft;
public listbox拖放Item()
{
InitializeComponent();
listBox1.AddHandler(ListBox.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(listBox1_MouseLeftButtonDown), true);
//注意此处不能加listBox1_MouseLeftButtonUp的处理了,否则MouseLeftButtonUp会被触发两次
//listBox1.AddHandler(ListBox.MouseLeftButtonUpEvent,
// new MouseButtonEventHandler(listBox1_MouseLeftButtonUp), true);
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
listBox1.Items.Add("选项1");
listBox1.Items.Add("选项2");
listBox1.Items.Add("选项3");
}
private void listBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
trackingMouseMove = true;
if (null != element)
{
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
double dblListBox2Left = (double)listBox2.GetValue(Canvas.LeftProperty);
double dblListBox2Top = (double)listBox2.GetValue(Canvas.TopProperty);
if (newLeft >= dblListBox2Left && newLeft <= (dblListBox2Left + listBox2.Width))
{
if (newTop >= dblListBox2Top && newTop <= (dblListBox2Top + listBox2.Height))
{
listBox2.Items.Add(listBox1.SelectedItem.ToString());
}
}
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
//清空坐标
newTop = 0;
newLeft = 0;
element.Cursor = null;
}
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
//记录鼠标位置
newTop = e.GetPosition(null).Y;
newLeft = e.GetPosition(null).X;
}
}
}
xaml代码
<navigation:Page x:Class="Silverlight拖放.listbox添加项后的拖放"
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"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="listbox添加项后的拖放 Page" Loaded="Page_Loaded">
<Canvas Background="#46461F">
<ListBox Canvas.Left="27" Canvas.Top="23" Height="187" Name="listBox1" Width="187" SelectionMode="Multiple" MouseLeftButtonDown="listBox1_MouseLeftButtonDown" MouseLeftButtonUp="listBox1_MouseLeftButtonUp" MouseMove="listBox1_MouseMove" />
</Canvas>
</navigation:Page>