2015-01-21 8 views
2

아래와 같이 오른쪽 아래 모서리에 엄지 손가락으로 크기를 조정하려는 팝업이 있습니다. 엄지 손가락에는 크기 조정을 수행하려는 첨부 된 동작이 있습니다.첨부 된 동작을 사용하여 Thumb을 통해 팝업 크기 조정

<Popup x:Name="Popbox" Placement="Mouse" StaysOpen="False" Width="50" Height="50" > 
    <Grid> 
     <Border Background="AliceBlue"/> 
     <Thumb HorizontalAlignment="Right" 
       VerticalAlignment="Bottom" 
       Width="16" Height="16" > 
      <i:Interaction.Behaviors> 
       <helpers:PopupResizeBehaviors PopupObject="{Binding ElementName=Popbox}"/> 
      </i:Interaction.Behaviors> 
     </Thumb> 
    </Grid> 
</Popup> 


class PopupResizeBehaviors : Behavior<Thumb> 
{ 
    private bool mouseDown; 
    private Point oldMousePosition; 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     AssociatedObject.PreviewMouseLeftButtonDown += (s, e) => 
     { 
      mouseDown = true; 
     }; 

     AssociatedObject.DragDelta += (s, e) => 
     { 
      if (!mouseDown) return; 

      double tempWidth = 0; 
      double tempHeight = 0; 
      PopupObject.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); 
      tempWidth = PopupObject.DesiredSize.Width; 
      tempHeight = PopupObject.DesiredSize.Height; 

      double yadjust = tempHeight + e.VerticalChange; 
      double xadjust = tempWidth + e.HorizontalChange; 

      PopupObject.Width = xadjust; 
      PopupObject.Height = yadjust; 
     }; 

     AssociatedObject.PreviewMouseLeftButtonUp += (s, e) => 
     { 
      mouseDown = false; 
     }; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
    } 

    public static readonly DependencyProperty PopupObjectProperty = 
     DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null)); 

    public Popup PopupObject 
    { 
     get { return (Popup)GetValue(PopupObjectProperty); } 
     set { SetValue(PopupObjectProperty, value); } 
    } 
} 

현재 작동하지는 않지만 내가 원하는 대상에 대한 좋은 아이디어를 제공해야합니다.

어떻게 작동합니까?

+0

'PopupObject'가 연결된 속성 인 경우 (동작 클래스에서 정의 된)'GetPopupObject' 및'SetPopupObject' 정적 메서드가 필요하지 않습니다? –

+0

아니야 괜찮아. 실제 드래그 및 크기 조정이 문제입니다. – Hank

답변

0

이것은 내가 결국 사용한 것입니다.

class PopupResizeBehaviors : Behavior<Thumb> 
{ 
    private const int MaxSize = 500; 
    private const int MinSize = 50; 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     AssociatedObject.DragDelta += (s, e) => 
     { 

      Thumb t = s as Thumb; 

      if (t.Cursor == Cursors.SizeWE || t.Cursor == Cursors.SizeNWSE) 
      { 
       PopupObject.Width = Math.Min(MaxSize, 
        Math.Max(PopupObject.Width + e.HorizontalChange, 
        MinSize)); 
      } 

      if (t.Cursor == Cursors.SizeNS || t.Cursor == Cursors.SizeNWSE) 
      { 
       PopupObject.Height = Math.Min(MaxSize, 
        Math.Max(PopupObject.Height + e.VerticalChange, 
        MinSize)); 
      } 
     }; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
    } 

    public static readonly DependencyProperty PopupObjectProperty = 
     DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null)); 

    public Popup PopupObject 
    { 
     get { return (Popup)GetValue(PopupObjectProperty); } 
     set { SetValue(PopupObjectProperty, value); } 
    } 
}