WPF:通过DependencyObject限制拖动操作的有效控制部件

WPF:通过DependencyObject限制拖动操作的有效控制部件,wpf,user-controls,drag-and-drop,Wpf,User Controls,Drag And Drop,我是在读了StackOverflow之后问这个问题的 提供的答案非常适用于允许您移动整个控件。但假设我有一个像这样创建的用户控件: <UserControl x:Class="WpfTest.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Heig

我是在读了StackOverflow之后问这个问题的

提供的答案非常适用于允许您移动整个控件。但假设我有一个像这样创建的用户控件:

<UserControl x:Class="WpfTest.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Border Background="Blue">
        <Grid>
            <Grid.ColumnDefinitions>

            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Canvas Grid.Row="0" Background="orange"></Canvas>
            <Canvas Grid.Row="1" Background="Purple"></Canvas>
        </Grid>
    </Border>
</UserControl>

我只希望在鼠标单击控件的标题部分Grid.Row[0](橙色部分)时控件移动。我不希望控件在单击控件的其余部分时响应拖动操作


我怎样才能做到这一点呢?

我在CodeProject上找到了一个非常好的例子,叫做DiagramDesigner。这会让你从正确的方向开始

假设您使用的是
DraggableExtender.CanDrag
附加属性,您可以设置一个触发器,根据鼠标是否在橙色标题上打开和关闭此属性:

<UserControl x:Class="WpfTest.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
    <UserControl.Style>
        <Style>
            <Setter Property="(DraggableExtender.CanDrag)" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsMouseOver, ElementName=headerCanvas}" Value="True">
                    <Setter Property="(drag:DraggableExtender.CanDrag)" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
    <Border Background="Blue">
        <Grid>
            <Grid.ColumnDefinitions>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Canvas x:Name="headerCanvas" Grid.Row="0" Background="orange"></Canvas>
            <Canvas Grid.Row="1" Background="Purple"></Canvas>
        </Grid>
    </Border>
</UserControl>

所以,在仔细研究之后,我解决了我的问题。解决方法是使用拇指

我的UserControl XAML如下所示:

<UserControl x:Class="WpfTest.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Border Background="Blue">
        <Grid>
            <Grid.ColumnDefinitions>

            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Canvas Grid.Row="0" Background="orange"></Canvas>
            <Thumb Grid.Row="0" Background="Cyan" DragCompleted="OnDragComplete" DragStarted="OnDragStarted" DragDelta="OnDragDelta" ></Thumb>
            <Canvas Grid.Row="1" Background="Purple"></Canvas>
        </Grid>
    </Border>
</UserControl>
public partial class UserControl1 : UserControl
    {
        private bool _isDragging;
        private double _x;
        private double _y;

        public UserControl1()
        {
            InitializeComponent();
        }

        private void OnDragComplete(object sender, DragCompletedEventArgs e)
        {
            _isDragging = false;

        }

        private void OnDragStarted(object sender, DragStartedEventArgs e)
        {
            _isDragging = true;

            _x = (double)GetValue(Canvas.LeftProperty);
            _y = (double)GetValue(Canvas.TopProperty);

        }

        private void OnDragDelta(object sender, DragDeltaEventArgs e)
        {
            if (!_isDragging) return;

            _x += e.HorizontalChange;
            _y += e.VerticalChange;


            SetValue(Canvas.LeftProperty,_x );
            SetValue(Canvas.TopProperty,_y );
        }
    }

这正是我想要的。还有一个问题:当我移动usercontrol时,我看到它被撕裂了。这不是一个平滑的绘图效果,我似乎找不到消除这种效果的方法。有什么线索吗?

对不起,杰夫,但我没有看到任何与这个问题有关的东西。谢谢罗伯特。我想我用拇指找到了更合我意的东西(在我发布那个问题之前我不知道)。尽管如此,还是要感谢你的回答;我可以在另一个控件上使用它。