我是WPF的新手,我正在尝试在我的应用程序中开发拖放功能。我有两个用户控件Window1和Window2。
如何通过单击橙色区域来启用任何此用户控件的拖放操作。只应启用拖放用户控件的橙色区域。灰色区域不应该启用拖放。我的意思是如果我们要拖动用户控件然后我们必须单击橙色区域,灰色区域也将被拖动。如果我们点击灰色区域,则用户控制不应移动。
请检查下面的图片,以帮助理解问题。
https://www.dropbox.com/sh/wj9mcbyi9wpcxgq/AAAb9r_aWxKm2Eah3PrT__5sa?dl=0
提前致谢。
答案 0 :(得分:1)
如果你想自己动手,那么你可以在这里使用我的实现:https://stackoverflow.com/a/17014906/145757
否则,您可以查看包含MouseDragElementBehavior
的 Blend SDK 。
答案 1 :(得分:1)
非常感谢@Pragmateek的帮助,它就像一个魅力。以下是我执行的代码。
我有一个用户控件UCDragme
,它有一个专用的橙色拖动区域TBDragme
。
<UserControl x:Class="NSR3.UCDragme"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="300">
<Grid Background="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Background="Orange" x:Name="TBDragme" Text="Dragme" />
<TextBlock Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Background="AliceBlue" Margin="20" Text="No drag"></TextBlock>
</Grid>
</UserControl>
主窗口Home
:
<Window x:Class="NSR3.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Home"
xmlns:nsr="clr-namespace:NSR3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Canvas x:Name="panel">
</Canvas>
<Button Content="Add" Grid.Row="1" Grid.Column="0" Click="Button_Click" />
</Grid>
</Window>
拖放Home
窗口代码隐藏引擎:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace NSR3
{
/// <summary>
/// Interaction logic for Home.xaml
/// </summary>
public partial class Home : Window
{
UCDragme box;
public Home()
{
InitializeComponent();
box = new UCDragme();
TextBlock tb = (TextBlock)box.FindName("TBDragme");
tb.MouseLeftButtonDown += box_MouseLeftButtonDown;
tb.MouseLeftButtonUp += box_MouseLeftButtonUp;
tb.MouseMove += box_MouseMove;
panel.Children.Add(box);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
box = new UCDragme();
TextBlock tb=(TextBlock)box.FindName("TBDragme");
tb.MouseLeftButtonDown += box_MouseLeftButtonDown;
tb.MouseLeftButtonUp += box_MouseLeftButtonUp;
tb.MouseMove += box_MouseMove;
panel.Children.Add(box);
}
private TextBlock draggedBox;
private Point clickPosition;
private void box_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
draggedBox = sender as TextBlock;
clickPosition = e.GetPosition(draggedBox);
draggedBox.CaptureMouse();
}
private void box_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
draggedBox.ReleaseMouseCapture();
draggedBox = null;
//box = null;
}
private void box_MouseMove(object sender, MouseEventArgs e)
{
if (draggedBox != null)
{
Point currentPosition = e.GetPosition(panel);
box.RenderTransform = draggedBox.RenderTransform as TranslateTransform ?? new TranslateTransform();
TranslateTransform transform = box.RenderTransform as TranslateTransform;
transform.X = currentPosition.X - clickPosition.X - draggedBox.Margin.Left;
transform.Y = currentPosition.Y - clickPosition.Y - draggedBox.Margin.Right;
}
}
}
}