我创建了一个代表图形边缘的自定义控件。中间是显示的重量。
(圆是顶点,连接线是边)
我通过使用重写的 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>
CODEBehind 用于控制:
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/TextBox 显示重量?