2010-11-30 7 views
1

라인 모양 좌표 (X1, Y1) 및 (X2, Y2)와 비슷한 WPF UserControlSegmentControl을 빌드 할 수 있습니까?라인 사용자 정의 컨트롤 빌드

는 이미 사용자 정의 라인 Shape을 구축,하지만 난 그것에 텍스트와 총알 같은 몇 가지 추가 사용자 정의 요소를 추가 때문에, UserControl이 필요합니다.

나는 몇 가지 코드를 내장,하지만 난 도움이 필요하다고 생각 :

<UserControl> 
<!-- internal Canvas; The UsrCtrl Parent is supposed to be a Canvas too --> 
    <Canvas> 
     <Line x:Name="line" Stroke="Black" StrokeThickness="1"></Line> 
     <Label x:Name="label" Content="Paris - Moscow"/> 
    </Canvas> 
</UserControl> 

* .cs

public partial class SegmentControl : UserControl 
{ 
    #region dependency properties 
    public static readonly DependencyProperty X1Property; 

... 
static void OnXYChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
{ 
    SegmentControl s = source as SegmentControl; 
    UpdateControlPositionAndSize(s); 
} 

static void UpdateControlPositionAndSize(SegmentControl sc) 
{ 
    double left = Math.Min(sc.X1, sc.X2); 
    double top = Math.Min(sc.Y1, sc.Y2); 

    double width = sc.X2 - sc.X1; 
    double height = sc.Y2 - sc.Y1; 

    Canvas.SetLeft(sc, left); 
    Canvas.SetTop(sc, top); 
    sc.Width = width; 
    sc.Height = height; 

    sc.line.X1 = sc.X1; // ?? 
    sc.line.Y1 = sc.Y1; // ?? 
    sc.line.X2 = sc.X2; // ??  
    sc.line.Y2 = sc.Y2; // ?? 
} 
+0

및 X2의 Y2는 이러한 맥락에서 의미

<UserControl> <Canvas> <Line x:Name="line" Stroke="Black" StrokeThickness="1" X1="{Binding RelativeSource={RelativeSource AncestorType={x:Type my:SegmentControl}}, Path=StartPosition.X}" Y1="{Binding RelativeSource={RelativeSource AncestorType={x:Type my:SegmentControl}}, Path=StartPosition.Y}" X2="{Binding RelativeSource={RelativeSource AncestorType={x:Type my:SegmentControl}}, Path=EndPosition.X}" Y2="{Binding RelativeSource={RelativeSource AncestorType={x:Type my:SegmentControl}}, Path=EndPosition.Y}" /> <Label x:Name="label" Content="{Binding={RelativeSource RelativeSource AncestorType={x:Type my:SegmentControl}}, Path=Text}"/> </Canvas> </UserControl> 

당신은 그것을 이런 식으로 뭔가를 사용합니다 :

귀하의 UserControl이 같은 것? 그들은 당신의 통제 안에 당신의 세그먼트의 좌표입니까? –

+0

@Nicolas Repiquet x1, y1, x2 및 y2는 선 모양과 동일한 의미입니다. 선의 좌표 (캔버스 내 구체적인 경우)의 "극단적 인"점. – serhio

답변

3

무엇 당신이 필요로하는 포인트의 UserControl을에서 사용자 정의 DependencyProperty을 만들고, 바인딩에 대한 그것의 라인 위치?

X1의 Y1을 무엇
<my:SegmentControl 
    StartPosition="{Binding Path=StartPoint}" 
    EndPosition="{Binding Path=EndPoint}" 
    Text="{Binding Path=SegmentText}" /> 
+0

이 코드는 멋지지만 StartPosition (0,0)을 사용하면 컨테이너의 왼쪽 위 모서리에 줄이 있어야합니다 ... "줄"위치가 UC에 비례하기 때문에이 경우가 아닙니다. StartPosition - UC 부모를 등록하십시오. 또한 UC Canvas.Left 등의 속성을 바인드하는 방법이 있어야합니다.) – serhio

+0

라인의 X1 : Y1을 StartPosition에 바인딩하는 대신 UserControl의 Canvas.Top과 Canvas.Left'를 StartPosition으로 설정하고 X1 : Y1을 0 : 0으로 설정합니다. 라인의 X2 : Y2를 EndPosition에 바인딩 된 상태로 둡니다. – Rachel