我創建了一個代表圖形邊緣的自定義控件。在中間是重量顯示。在行的中間添加標籤
(Circules是頂點和所述連接線是邊緣)
我通過繪製重量與重寫的OnRender方法制造這一點。但這不是一個好的解決方案。
沒有辦法通過文本框使權重可編輯。所以,如果我可以將TextBox或ContentPresenter添加到重寫的OnRender方法以便使權重可編輯,那將會很棒。但我不知道該怎麼做。
無論如何,這是我目前的狀態:
<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>
代碼隱藏的控制:
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 /文本框顯示的重量?
請勿在WPF中重寫'OnRender()'。這不是winforms。爲什麼不在ControlTemplate中添加所需的UI元素? –
我不知道如何在行旁邊添加UI元素。另外UI元素應該放置在水平線的一半處。 – user885679