2010-11-30 55 views
1

是否有可能建立一個WPF 用戶控件SegmentControl,它與線形座標(X1,Y1)和(X2,Y2)相似?建立一條線用戶控件

我已經建立了一個自定義行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

這是什麼X1 Y1和Y2 X2是指在這方面?他們是否在您的控制範圍內協調了您的細分? – 2010-11-30 19:04:17

+0

@Nicolas Repiquet x1,y1,x2和y2的含義與它所表示的線形相同。該行的座標(在我的具體情況下在畫布中)「極端」點。 – serhio 2010-11-30 19:15:18

回答

3

什麼只是你所需要的積分的用戶控件創建自定義DependencyProperty,並結合你的線路位置呢?

你的用戶控件會是這樣的:

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

你會使用它是這樣的:

<my:SegmentControl 
    StartPosition="{Binding Path=StartPoint}" 
    EndPosition="{Binding Path=EndPoint}" 
    Text="{Binding Path=SegmentText}" />