2009-12-25 5 views
1

나는이 두 가지 자원에서 패널 클래스를 작성하려고 해요 :WPF 드래그 가능한 패널 클래스

패널 클래스는 것 두 개의 연결된 속성 "X"와 "Y"이고 어떤 원소가 x와 y에 0을 준다면 패널의 중앙에 놓일 것입니다. 이 패널을 통해 사용자는 주변을 끌 수 있습니다. 이 수업을 도와주세요. 나는 WPF에 대해 매우 익숙하다.


이것은 내가 얼마나 멀리 왔는지입니다. 이제는 구현하려고 시도했지만 작동하지 않습니다. GetTop, GetLeft, GetBottom, GetRight 함수는 기본적으로 패널 클래스에 정의되어 있지 않으며 필요한 것입니다. 이 4 가지 방법이있는 경우 여기에서 드래그 기능을 구현할 수 있습니다.

using System; 
using System.Linq; 
using System.Windows; 
using System.ComponentModel; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace SmartERP.Elements 
{ 
    public class SmartCanvas : Panel 
    { 
     public static readonly DependencyProperty TopProperty; 
     public static readonly DependencyProperty LeftProperty; 
     public static readonly DependencyProperty BottomProperty; 
     public static readonly DependencyProperty RightProperty; 

     static SmartCanvas() 
     { 
      TopProperty = DependencyProperty.Register("Top", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0)); 
      LeftProperty = DependencyProperty.Register("Left", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0)); 
      BottomProperty = DependencyProperty.Register("Bottom", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0)); 
      RightProperty = DependencyProperty.Register("Right", typeof(double), typeof(SmartCanvas), new PropertyMetadata(0.0)); 
     } 

     public double Top 
     { 
      get { return (double)base.GetValue(TopProperty); } 
      set { base.SetValue(TopProperty, value); } 
     } 

     public double Bottom 
     { 
      get { return (double)base.GetValue(BottomProperty); } 
      set { base.SetValue(BottomProperty, value); } 
     } 

     public double Left 
     { 
      get { return (double)base.GetValue(LeftProperty); } 
      set { base.SetValue(LeftProperty, value); } 
     } 

     public double Right 
     { 
      get { return (double)base.GetValue(RightProperty); } 
      set { base.SetValue(RightProperty, value); } 
     } 

     private double GetTop(UIElement element) 
     { 
      return (double)this.GetValue(TopProperty); 
     } 

     private double GetLeft(UIElement element) 
     { 
      return (double)this.GetValue(LeftProperty); 
     } 

     private double GetBottom(UIElement element) 
     { 
      return (double)this.GetValue(BottomProperty); 
     } 

     private double GetRight(UIElement element) 
     { 
      return (double)this.GetValue(RightProperty); 
     } 

     protected override Size ArrangeOverride(Size arrangeSize) 
     { 
      Point middle = new Point(arrangeSize.Width/2, arrangeSize.Height/2); 

      foreach (UIElement element in base.InternalChildren) 
      { 
       if (element == null) 
       { 
        continue; 
       } 
       double x = 0.0; 
       double y = 0.0; 
       double left = GetLeft(element); 
       if (!double.IsNaN(left)) 
       { 
        x = left; 
       } 

       double top = GetTop(element); 
       if (!double.IsNaN(top)) 
       { 
        y = top; 
       } 

       element.Arrange(new Rect(new Point(middle.X + x, middle.Y + y), element.DesiredSize)); 
      } 
      return arrangeSize; 
     } 
    } 
} 
+0

지금까지 무엇을 가지고 계신가요? – Heinzi

+0

어떻게 구현할 수 있습니까? 도와주세요. –

+0

Soham, 당신은 구현하려는 것을 설명하지 않았습니다. 우리는 질문이 무엇인지 모를 때 어떻게 대답 할 수 있습니까? 무엇을 성취하려고합니까? 귀하의 코드는 현재 무엇을하고 있습니까? 기대 한 행동과 어떻게 다른가? 맞춤 패널은 어떻게 사용됩니까 (예제 코드 포함)? 이러한 것들을 설명하면 여기 누군가가 올바른 방향으로 당신을 가리킬 수 있습니다. –

답변

1

패널 클래스는 두 개의 연결된 속성 "X"와 "Y"를해야합니다 [...]

OK, 당신은 그 연결된 속성을 구현해야합니다. MSDN의 사용자 지정 연결 속성 (Attached Properties Overview)의 예를 참조하십시오. 다음은이 X를 찾을 것입니다 방법은 다음과 같습니다

public static readonly DependencyProperty XProperty = 
    DependencyProperty.RegisterAttached("X", typeof(double), 
     typeof(SmartCanvas), new FrameworkPropertyMetadata(0.0)); 

public static void SetX(UIElement element, double value) { element.SetValue(XProperty, value); } 
public static double GetX(UIElement element) { return (double)element.GetValue(XProperty); } 

이 작업을 완료 한 후에는 GetXGetY을 가지고, 당신은 GetTopGetLeft 무엇을 의미하는 아마이다.

+0

이제 RegisterAttached를 사용하고 있지만 캔버스 주위로 물건을 드래그하거나 캔버스 중앙에 요소의 중심을 배치 할 수 없습니다. 도와 주시고 첨부 된 속성으로 캔버스 중심에 배치하는 더 좋은 방법을 제안 할 수 있다면 구현 방법을 알려주십시오. 도와주세요. –

+0

왜 드래그 앤 드롭 할 수 있어야합니까? 코드에는 드래그 앤 드롭과 관련된 내용이 없습니다! 어쩌면 짧은 정리가 순서대로 이루어집니다. 여기서 사람들은 (a) 코드에서 버그를 찾고, (b) 문제를 해결하기 위해 올바른 길을 찾고, (c) WPF 개념을 이해하는 데 도움이되도록 기꺼이 도와 줄 것입니다. 아마도 * 얻을 수없는 것은 : "X가있는 프로그램이 필요합니다. 제게 쓰십시오." – Heinzi

+0

OK, 문제로 돌아 가기 : Canvas 대신 Panel을 기본 클래스로 사용하는 이유는 무엇입니까? 귀하가 질문에 링크 한 드래그 예제는 왼쪽, 위 등의'Canvas' 속성을 필요로합니다.이 속성은 패널에 몇 가지 코드가 첨부되어있을 수 있습니다 (패널을 사용하여 동일한 효과를 만들지 않고 첨부 된 속성을 다시 만들 수 없습니다) . – Heinzi