그래프의 가장자리를 나타내는 사용자 정의 컨트롤을 만들었습니다. 가운데에 체중이 표시됩니다.라인 중간에 레이블 추가
은 (Circules 정점이며 연결 라인 에지 임)
내가의 OnRender 메소드를 재정의 중량 드로잉하여 만들었다. 그러나 이것은 좋은 해결책이 아닙니다.예를 들어 텍스트 상자로 가중치를 편집 할 수있는 방법이 없습니다. 따라서 가중치를 편집 가능하게 만들기 위해 재정의 된 OnRender 메서드에 TextBox 또는 ContentPresenter를 추가 할 수 있다면 좋을 것입니다. 그러나 나는 이것을 어떻게하는지 모른다.
public class Edge : Control
{
static Edge()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Edge), new FrameworkPropertyMetadata(typeof(Edge)));
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
Point p = new Point((PositionV.X + PositionU.X)/2 + 4, (PositionV.Y + PositionU.Y)/2);
drawingContext.DrawText(new FormattedText(Weight != null ? Weight.ToString() : "",
System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily.ToString()),
this.FontSize, this.Foreground), p);
}
public int Weight
{
get { return (int)GetValue(WeightProperty); }
set { SetValue(WeightProperty, value); }
}
// Using a DependencyProperty as the backing store for Weight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WeightProperty =
DependencyProperty.Register("Weight", typeof(int), typeof(Edge), new UIPropertyMetadata(0));
/// <summary>
/// Gets or sets the value of the position from the correspondending U Vertex control
/// </summary>
public Point PositionU
{
get { return (Point)GetValue(PositionUProperty); }
set { SetValue(PositionUProperty, value); }
}
// Using a DependencyProperty as the backing store for PositionU. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PositionUProperty =
DependencyProperty.Register("PositionU", typeof(Point), typeof(Edge), new UIPropertyMetadata(new Point()));
/// <summary>
/// Gets or sets the value of the position from the correspondending V Vertex control
/// </summary>
public Point PositionV
{
get { return (Point)GetValue(PositionVProperty); }
set { SetValue(PositionVProperty, value); }
}
// Using a DependencyProperty as the backing store for PositionV. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PositionVProperty =
DependencyProperty.Register("PositionV", typeof(Point), typeof(Edge), new UIPropertyMetadata(null));
}
가 어떻게 라인의 중간에있는 TextBlock/텍스트 상자에 무게를 표시 할 수 있습니다 컨트롤
<Style TargetType="{x:Type local:Edge}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Edge}">
<DockPanel>
<Line Stroke="{TemplateBinding Foreground}"
X1="{Binding Mode=TwoWay,Path=PositionU.X,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:Edge}}}"
Y1="{Binding Mode=TwoWay,Path=PositionU.Y,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:Edge}}}"
X2="{Binding Mode=TwoWay,Path=PositionV.X,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:Edge}}}"
Y2="{Binding Mode=TwoWay,Path=PositionV.Y,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:Edge}}}" >
</Line>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
코드 숨김 :
어쨌든,이 내 현재 상태입니까?
WPF에서'OnRender()'를 재정의하지 마십시오. 이것은 winforms가 아닙니다. 왜 ControlTemplate에 원하는 UI 요소를 추가하지 않습니까? –
줄 옆에 UI 요소를 추가하는 방법을 모르겠습니다. 또한 UI 요소는 선의 절반에 수평으로 배치해야합니다. – user885679