Построить линию UserControl - PullRequest
       0

Построить линию UserControl

1 голос
/ 30 ноября 2010

Можно ли построить WPF UserControl 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; // ??
}

1 Ответ

3 голосов
/ 30 ноября 2010

А как насчет создания пользовательского DependencyProperty в UserControl необходимых вам точек и привязки к нему вашей позиции Line?

Ваш UserControl будет выглядеть примерно так:

<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}" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...