2015-12-30 3 views
1

Я пытаюсь создать пользовательский расширяемый движок для моего проекта WPF. В идеале, если пользователь нажимает кнопку расширителя, он/она должен иметь возможность расширения. Если щелкнуть и перетащить текстовое поле с надписью «Дополнительные параметры», я хочу, чтобы этот человек мог перемещать расширитель. Мой код не работает и просто расширяет расширитель. Я попытался связать клик по заголовку с чем-то другим, чем расширением, но это, похоже, не работает. Ниже приведены коды xaml и C#. Помогите?Move-able Expander

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

ответ

0

Я сделал то же самое, используя 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>  

View Model

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

Mouse Поведения как прикреплено свойства

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 
}