【问题标题】:Move-able Expander可移动扩展器
【发布时间】:2015-12-30 14:50:53
【问题描述】:

我正在尝试为我的 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();
    }

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    我使用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
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多