2010-11-30 54 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}" />