代码之家  ›  专栏  ›  技术社区  ›  Chris Holmes

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

  •  0
  • Chris Holmes  · 技术社区  · 17 年前

    我在读完之后问这个问题 this answer

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

    3 回复  |  直到 9 年前
        1
  •  1
  •   Robert Macnee    17 年前

    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>
    
        2
  •  1
  •   Chris Holmes    17 年前

    .

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

        3
  •  -1
  •   jeff    17 年前

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

    推荐文章