Combining LineGeometry with EllipseGeometry (in code, not in XAML)

I am trying to create a custom form using WPF. For starters, I was just trying to create a simple line that has a circle at each end (I know there are LineCaps, but that's not what I'm looking for).

I have learned some lessons and the easiest way to do this seems to be using CombinedGeometry . However, I cannot get it to work correctly. Here is my code that creates the geometry object:

 protected override Geometry DefiningGeometry { get { Point ellipseCenter1 = new Point(X1 - this.CapDiameter / 2, Y1 - this.CapDiameter / 2); Point ellipseCenter2 = new Point(X2 - this.CapDiameter / 2, Y2 - this.CapDiameter / 2); var ellipse1 = new EllipseGeometry(ellipseCenter1, CapDiameter, CapDiameter); var ellipse2 = new EllipseGeometry(ellipseCenter2, CapDiameter, CapDiameter); var line = new LineGeometry(this.StartPoint, this.EndPoint); var combined1 = new CombinedGeometry(GeometryCombineMode.Union, ellipse1, line); var combined2 = new CombinedGeometry(GeometryCombineMode.Union, combined1, ellipse2); // Freeze the geometry for performance benefits combined2.Freeze(); return combined2; } } 

However, for some reason this does not output a string. He draws both circles, but not a line. Obviously I missed something, can someone please indicate what? :)

Just in case, this is important, here is the rest of the class:

 #region Dependency Properties public static readonly DependencyProperty X1Property = DependencyProperty.Register( "X1", typeof(double), typeof(CappedLine), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure ) ); public static readonly DependencyProperty Y1Property = DependencyProperty.Register( "Y1", typeof(double), typeof(CappedLine), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure ) ); public static readonly DependencyProperty X2Property = DependencyProperty.Register( "X2", typeof(double), typeof(CappedLine), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure ) ); public static readonly DependencyProperty Y2Property = DependencyProperty.Register( "Y2", typeof(double), typeof(CappedLine), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure ) ); public static readonly DependencyProperty CapDiameterProperty = DependencyProperty.Register( "CapDiameter", typeof(double), typeof(CappedLine), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure ) ); #endregion #region CLR Properties public double X1 { get { return (double)base.GetValue(X1Property); } set { base.SetValue(X1Property, value); } } public double Y1 { get { return (double)base.GetValue(Y1Property); } set { base.SetValue(Y1Property, value); } } public double X2 { get { return (double)base.GetValue(X2Property); } set { base.SetValue(X2Property, value); } } public double Y2 { get { return (double)base.GetValue(Y2Property); } set { base.SetValue(Y2Property, value); } } public Point StartPoint { get { return (new Point(X1, Y1)); } } public Point EndPoint { get { return (new Point(X2, Y2)); } } public double CapDiameter { get { return (double)base.GetValue(CapDiameterProperty); } set { base.SetValue(CapDiameterProperty, value); } } #endregion 

And here is the XAML code that I used to verify it:

 <Window x:Class="HG.WPF.Shapes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:shapes="clr-namespace:Tomers.WPF.Shapes" Title="WPF Custom Shapes" Height="300" Width="300" ResizeMode="NoResize"> <Canvas Background="Black"> <shapes:CappedLine X1="30" Y1="30" X2="200" Y2="200" CapDiameter="5" Fill="White" Stroke="White" StrokeThickness="2"></shapes:CappedLine> </Canvas> </Window> 
+4
source share
1 answer

LineGeometry objects LineGeometry disappear if you try to execute Union them. From MSDN:

The GeometryCombineMode property indicates how the two geometries will be combined. Note that CombinedGeometry combines the region specified in the two geometries, so geometries that do not have a region (such as LineGeometry) disappear when combined.

You can use GeometryGroup :

 GeometryGroup combined = new GeometryGroup(); combined.Children.Add(ellipse1); combined.Children.Add(ellipse2); combined.Children.Add(line); 

or you can first use CombinedGeometry for ellipse objects and then group them together with a line using a GeometryGroup .

+6
source

Source: https://habr.com/ru/post/1479520/


All Articles