可移动扩展器

本文关键字:扩展器 可移动 | 更新日期: 2023-09-27 18:28:33

我正试图为我的WPF项目创建一个用户可移动的扩展器。理想情况下,如果用户单击扩展器按钮,他/她应该能够进行扩展。如果有人点击并拖动写着"高级选项"的文本框,我希望那个人能够移动扩展器。我的代码不起作用,只是扩展了扩展器。我试着将点击标题与展开不同的内容联系起来,但这似乎不起作用。xaml和C#代码如下。帮助

    <Expander HorizontalAlignment="Left" Margin="89,372,0,0" VerticalAlignment="Top" Height="453" Width="909" IsExpanded="True" x:Name="grid_expander">
        <Expander.Header>
            <TextBlock HorizontalAlignment="Left" Height="22" Margin="5,0,0,0" TextWrapping="Wrap" Width="154" Foreground="White" FontSize="18" MouseDown="grid_expander_MouseDown" MouseUp="grid_expander_MouseUp" MouseMove="grid_expander_MouseMove">
                Advanced Options
            </TextBlock>
        </Expander.Header>
        <Expander.RenderTransform>
            <TranslateTransform x:Name="expander_xy"/>
        </Expander.RenderTransform>
    </Expander>




    private void grid_expander_MouseDown(object sender, MouseButtonEventArgs e)
    {
        //e.Handled = true;
        m_start = e.GetPosition(window);
        m_startOffset = new Vector(expander_xy.X, expander_xy.Y);
        grid_expander.CaptureMouse();
    }
    private void grid_expander_MouseMove(object sender, MouseEventArgs e)
    {
        if (grid_expander.IsMouseCaptured)
        {
            Vector offset = Point.Subtract(e.GetPosition(window), m_start);
            expander_xy.X = m_startOffset.X + offset.X;
            expander_xy.Y = m_startOffset.Y + offset.Y;
        }
    }
    private void grid_expander_MouseUp(object sender, MouseButtonEventArgs e)
    {
        grid_expander.ReleaseMouseCapture();
    }

可移动扩展器

我也使用了Image Control而不是Expander Control,我相信您可以替换Image Control并放置Expander Control。我将共享我的代码。尝试使用Expander Control。如果有帮助,请告诉我。

XAML

  <Grid 
        Background="Black"
        mousebehav:MouseBehaviour.MouseLeftButtonUpCommand="{Binding MLBUCommand}">
        <Canvas >
            <Image RenderTransformOrigin="0.5,0.5"                        
                   Source="{Binding ImageSource}"
                   Stretch="None"  SnapsToDevicePixels="False"
                   mousebehav:MouseBehaviour.MouseLeftButtonDownCommand="{Binding MDCommand}"
                   mousebehav:MouseBehaviour.MouseMoveCommand="{Binding MMCommand}">
                <Image.RenderTransform>
                    <TranslateTransform  
                        X="{Binding MouseX}" 
                        Y="{Binding MouseY}" />   
                </Image.RenderTransform>
                <Image.LayoutTransform>
                    <TransformGroup>
                        <ScaleTransform 
                            ScaleX="{Binding ScaleX}" 
                            ScaleY="{Binding ScaleY}"/>
                        <RotateTransform 
                            Angle="{Binding RotateAngle}"/>
                    </TransformGroup>
                </Image.LayoutTransform>
            </Image>
        </Canvas>
    </Grid>      

查看模型

private double _ScaleX;
    public double ScaleX
    {
        get { return _ScaleX; }
        set { _ScaleX = value; NotifyPropertyChanged(); }
    }
    private double _ScaleY;
    public double ScaleY
    {
        get { return _ScaleY; }
        set { _ScaleY = value; NotifyPropertyChanged(); }
    }
    private double _RotateAngle;
    public double RotateAngle {
        get { return _RotateAngle; }
        set { _RotateAngle = value; NotifyPropertyChanged(); }
    }
    private double _MouseX;
    public double MouseX
    {
        get { return _MouseX; }
        set { _MouseX = value; NotifyPropertyChanged(); }
    }
    private double _MouseY;
    public double MouseY
    {
        get { return _MouseY; }
        set { _MouseY = value; NotifyPropertyChanged(); }
    }
    public bool IsMouseCaptured { get; set; }
    private RelayCommand _mouseDownCommand;
    public RelayCommand MDCommand
    {
        get
        {
            if (_mouseDownCommand == null) return _mouseDownCommand = new RelayCommand(param => ExecuteMouseDown((MouseEventArgs)param));
            return _mouseDownCommand;
        }
        set { _mouseDownCommand = value; }
    }
    System.Windows.Point start;
    System.Windows.Point origin;
    private void ExecuteMouseDown(MouseEventArgs e)
    {
        var image = (System.Windows.Controls.Image)e.OriginalSource;
        var border = (IInputElement)image.Parent;
        start = e.GetPosition(border);
        origin = new System.Windows.Point(MouseX, MouseY);
        IsMouseCaptured = true;
    }
    private RelayCommand _mouseMoveCommand;
    public RelayCommand MMCommand
    {
        get
        {
            if (_mouseMoveCommand == null) return _mouseMoveCommand = new RelayCommand(param => ExecuteMouseMove((MouseEventArgs)param));
            return _mouseMoveCommand;
        }
        set { _mouseMoveCommand = value; }
    }
    private void ExecuteMouseMove(MouseEventArgs e)
    {
        if(IsMouseCaptured)
        {
            var image = (System.Windows.Controls.Image)e.OriginalSource;
            var border = (IInputElement)image.Parent;
            Vector v = start - e.GetPosition(border);
            MouseX = origin.X - v.X;
            MouseY = origin.Y - v.Y;
        }
    }
    private RelayCommand _MLBUCommand;
    public RelayCommand MLBUCommand
    {
        get
        {
            if (_MLBUCommand == null) return _MLBUCommand = new RelayCommand(param => Execute_MLBU((MouseEventArgs)param));
            return _MLBUCommand;
        }
        set { _MLBUCommand = value; }
    }
    private void Execute_MLBU(MouseEventArgs param)
    {
        IsMouseCaptured = false;
    }

鼠标行为作为附加属性

public class MouseBehaviour
{
    #region MouseUp
    public static readonly DependencyProperty MouseUpCommandProperty =
        DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseUpCommandChanged)));
    private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;
        element.MouseUp += element_MouseUp;
    }
    static void element_MouseUp(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;
        ICommand command = GetMouseUpCommand(element);
        command.Execute(e);
    }
    public static void SetMouseUpCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseUpCommandProperty, value);
    }
    public static ICommand GetMouseUpCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseUpCommandProperty);
    }
    #endregion
    #region MouseDown
    public static readonly DependencyProperty MouseDownCommandProperty =
        DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseDownCommandChanged)));
    private static void MouseDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;
        element.MouseDown += element_MouseDown;
    }
    static void element_MouseDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;
        ICommand command = GetMouseDownCommand(element);
        command.Execute(e);
    }
    public static void SetMouseDownCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseDownCommandProperty, value);
    }
    public static ICommand GetMouseDownCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseDownCommandProperty);
    }
    #endregion
    #region MouseMove
    public static readonly DependencyProperty MouseMoveCommandProperty =
        DependencyProperty.RegisterAttached("MouseMoveCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseMoveCommandChanged)));
    private static void MouseMoveCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;
        element.MouseMove += new MouseEventHandler(element_MouseMove);
    }
    static void element_MouseMove(object sender, MouseEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;
        ICommand command = GetMouseMoveCommand(element);
        command.Execute(e);
    }
    public static void SetMouseMoveCommand(UIElement element, ICommand value)
    {
        element.SetValue(MouseMoveCommandProperty, value);
    }
    public static ICommand GetMouseMoveCommand(UIElement element)
    {
        return (ICommand)element.GetValue(MouseMoveCommandProperty);
    }
    #endregion
 }