Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd Label in the middle of the line
    primarykey
    data
    text
    <p>i created a custom control which represents an edge of a graph. In the middle of the is the weight displayed.</p> <p><img src="https://i.stack.imgur.com/lFLZH.jpg" alt="enter image description here"> (Circules are vertices and the connecting line are the edges)</p> <p>I made this by drawing the weight with the overriden OnRender method. But this isn't a nice solution.</p> <p>There is no way to make the the weight editable for example by a textbox. So it would be great if I could add a TextBox or a ContentPresenter to the overriden OnRender method in order to make the weight editable. But I dont' know how to do this.</p> <p>Anyway, this is my current state:</p> <pre><code> &lt;Style TargetType="{x:Type local:Edge}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:Edge}"&gt; &lt;DockPanel&gt; &lt;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}}}" &gt; &lt;/Line&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>CODEBehind for the control:</p> <pre><code> 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)); /// &lt;summary&gt; /// Gets or sets the value of the position from the correspondending U Vertex control /// &lt;/summary&gt; 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())); /// &lt;summary&gt; /// Gets or sets the value of the position from the correspondending V Vertex control /// &lt;/summary&gt; 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)); } </code></pre> <p>How can I display the weight with a TextBlock/TextBox in the middle of the line?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload