我在WPF窗口中创建了2个按钮,并为两个按钮添加了鼠标按下和鼠标按下事件。我在一个按钮上按下鼠标,在第二个按钮上按下鼠标。但我在两个事件中获得相同的第一个按钮对象到事件处理程序。我的问题是为什么我没有在鼠标注册事件中获得第二个按钮对象。
这是我的XAML
<Window x:Class="MouseDownUpSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="but1" Content="source" HorizontalAlignment="Left" Margin="86,68,0,0" VerticalAlignment="Top" Width="75" PreviewMouseLeftButtonDown="Button_MouseDown" PreviewMouseLeftButtonUp="Button_MouseUp" />
<Button x:Name="but2" Content="destination" HorizontalAlignment="Left" Margin="406,164,0,0" VerticalAlignment="Top" Width="75" PreviewMouseLeftButtonDown="Button_MouseDown" PreviewMouseLeftButtonUp="Button_MouseUp"/>
</Grid>
代码
public partial class MainWindow : Window
{
string source = null;
string destination = null;
public MainWindow()
{
InitializeComponent();
}
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
Button src=sender as Button;
source = src.Content as string;
}
private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
Button src = sender as Button;
destination = src.Content as string;
if(destination.Equals(source))
{
}
}
我正试图通过拖放操作将数据从一个对象传输到另一个对象。降
答案 0 :(得分:2)
我的问题是为什么我没有在鼠标注册事件中获得第二个按钮对象。
因为这是按钮的工作方式。
取自MSDN:
如果在指针悬停在表单或控件上时按下鼠标按钮,该对象将“捕获”鼠标并接收所有鼠标事件,包括最后一个MouseUp事件。
您可能也会对此感兴趣:
如果连续按下鼠标按钮,第一次按下后捕获鼠标的对象将接收所有鼠标事件,直到释放所有按钮。
答案 1 :(得分:0)
Mouse.MouseDown
,释放任何鼠标按钮时会出现Mouse.MouseUp
。因此,当您单击按钮MouseUp
时,事件始终跟随MouseDown
事件,因为它们是连续事件。因此,在这种情况下,if()
条件始终为true
。
if(destination.Equals(source))
{
//always executed;
}
答案 2 :(得分:0)
我通过WPF拖拽实现了我的目标。使用DragDropEffects.Copy
删除XAML
<Window x:Class="MouseDownUpSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="but1" Content="source" HorizontalAlignment="Left" Margin="86,68,0,0" VerticalAlignment="Top"
Width="75" PreviewMouseMove="but_MouseMove" AllowDrop="True" PreviewDrop="but_Drop" />
<Button x:Name="but2" Content="destination" HorizontalAlignment="Left" Margin="406,164,0,0" VerticalAlignment="Top"
Width="75" PreviewMouseMove="but_MouseMove" AllowDrop="True" PreviewDrop="but_Drop"/>
</Grid>
CODE
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void but_MouseMove(object sender, MouseEventArgs e)
{
Button src=sender as Button;
if (src != null && e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(src,
src.Content.ToString(),
DragDropEffects.Copy);
}
}
private void but_Drop(object sender, DragEventArgs e)
{
Button dest = sender as Button;
string destinationContent = null;
destinationContent = dest.Content as string;
if (dest != null)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
string sourceContent = (string)e.Data.GetData(DataFormats.StringFormat);
if (destinationContent.Equals(sourceContent))
{
Console.WriteLine("equal");
}
}
}
}
}