0

在阅读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] (橙色部分)时,我只希望控件移动。单击控件的其余部分时,我不希望控件响应拖动操作。

我怎样才能做到这一点?

4

3 回答 3

1

假设您使用的是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>
于 2009-03-12T21:54:30.577 回答
1

所以,经过一番挖掘,我解决了我的问题。解决方案是使用Thumb

我的 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>

我们可以看到添加了不同颜色的拇指,它覆盖了橙色的标题(让我知道拇指实际上在那里)。

Thumb 事件的代码如下所示:

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 );
        }
    }

这正是我想要的。确实存在一个问题:当我移动它时,我看到用户控件有些撕裂。这不是一个平滑的绘图效果,我似乎无法找到消除它的方法。有什么线索吗?

于 2009-03-12T21:58:24.920 回答
-1

我在 CodeProject 上找到了一个非常棒的例子,叫做 DiagramDesigner。这应该让您朝着正确的方向开始。CodeProject, DiagramDesigner4

于 2009-03-12T20:47:14.903 回答