CustomControl XAML:ArcSegment.Size绑定到控件的实际宽度和高度

时间:2020-07-11 12:11:29

标签: c# wpf xaml binding

我的其他程序员!

我已经搜索了Internet,但是找不到解决方案。可能这是由于我是WPF的新手。我正在尝试实现以下目标:

CustomControl的{​​{1}}的{​​{1}}与ArcSegment的大小绑定: enter image description here

这应该说明带有裸片的晶圆(半导体行业)

因此,我了解到我需要在XAML中使用Size来构造“带有缺口的圆圈”

我的问题是我无法将CustomControl元素绑定到CustomControl的<Path>ArcSegment具有Size属性,而ArcSegment具有SizeCustomControl。我尝试了几种方法,但它们都不对我有用。

这是我ActualWidth的课程:

ActualHeight

以及我使用的XAML样式:

CustomControl

当然我也需要绑定 public class WaferMap : Control { public static readonly DependencyProperty ActualSizeProperty = DependencyProperty.Register( nameof(ActualSize), typeof(Size), typeof(WaferMap), new PropertyMetadata(default(Size))); public Size ActualSize { get => (Size) this.GetValue(ActualSizeProperty); set => this.SetValue(ActualSizeProperty, value); } public WaferMap() { var actualWidthPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ActualWidthProperty, typeof(WaferMap)); actualWidthPropertyDescriptor.AddValueChanged(this, OnActualWidthOrHeightChanged); var actualHeightPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ActualHeightProperty, typeof(WaferMap)); actualHeightPropertyDescriptor.AddValueChanged(this, OnActualWidthOrHeightChanged); } private void OnActualWidthOrHeightChanged(object? sender, EventArgs e) { this.ActualSize = new Size(ActualWidth, ActualHeight); } static WaferMap() { DefaultStyleKeyProperty.OverrideMetadata(typeof(WaferMap), new FrameworkPropertyMetadata(typeof(WaferMap))); } } ,但是首先我想看看简单的东西对我有用。不幸的是,事实并非如此。我在“输出”窗口中看到了这一点:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Zeiss.CdAnalizer.Ui">
    <Style TargetType="local:WaferMap">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:WaferMap" >
                    <ControlTemplate.Resources>
                        <local:WaferMap x:Key="WaferMap"/>
                    </ControlTemplate.Resources>
                    <Grid DataContext="{Binding Source={StaticResource WaferMap}}">
                        <Path Stroke="Black" StrokeThickness="1">
                            <Path.Data>
                                <PathGeometry>
                                    <PathGeometry.Figures>
                                        <PathFigureCollection>
                                            <PathFigure StartPoint="145,300">
                                                <PathFigure.Segments>
                                                    <PathSegmentCollection>
                                                        <ArcSegment 
                                                            IsLargeArc="True"
                                                            Size="{Binding Path=ActualSize, PresentationTraceSources.TraceLevel=High}"
                                                            SweepDirection="Clockwise"
                                                            Point="155,300" />
                                                    </PathSegmentCollection>
                                                </PathFigure.Segments>
                                            </PathFigure>
                                        </PathFigureCollection>
                                    </PathGeometry.Figures>
                                </PathGeometry>
                            </Path.Data>
                        </Path>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style></ResourceDictionary>

当我说这行不通时=当我改变表格的大小时,什么也没发生(弧形保持相同的小虚线)。 同样,永远不会调用StartPoint。当我更改表单的大小时,将调用setter(如您所见,我已经注册了System.Windows.Data Warning: 60 : BindingExpression (hash=14976165): Default mode resolved to OneWay System.Windows.Data Warning: 61 : BindingExpression (hash=14976165): Default update trigger resolved to PropertyChanged System.Windows.Data Warning: 62 : BindingExpression (hash=14976165): Attach to System.Windows.Media.ArcSegment.Size (hash=24211521) System.Windows.Data Warning: 64 : BindingExpression (hash=14976165): Use Framework mentor <null> System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 69 : BindingExpression (hash=14976165): Framework mentor not found System.Windows.Data Warning: 65 : BindingExpression (hash=14976165): Resolve source deferred System.Windows.Data Warning: 95 : BindingExpression (hash=14976165): Got InheritanceContextChanged event from ArcSegment (hash=24211521) System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 69 : BindingExpression (hash=14976165): Framework mentor not found System.Windows.Data Warning: 95 : BindingExpression (hash=14976165): Got InheritanceContextChanged event from ArcSegment (hash=24211521) System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 69 : BindingExpression (hash=14976165): Framework mentor not found System.Windows.Data Warning: 95 : BindingExpression (hash=14976165): Got InheritanceContextChanged event from ArcSegment (hash=24211521) System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 69 : BindingExpression (hash=14976165): Framework mentor not found System.Windows.Data Warning: 95 : BindingExpression (hash=14976165): Got InheritanceContextChanged event from ArcSegment (hash=24211521) System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 69 : BindingExpression (hash=14976165): Framework mentor not found System.Windows.Data Warning: 95 : BindingExpression (hash=14976165): Got InheritanceContextChanged event from ArcSegment (hash=24211521) System.Windows.Data Warning: 67 : BindingExpression (hash=14976165): Resolving source System.Windows.Data Warning: 70 : BindingExpression (hash=14976165): Found data context element: Path (hash=10232270) (OK) System.Windows.Data Warning: 78 : BindingExpression (hash=14976165): Activate with root item WaferMap (hash=14353717) System.Windows.Data Warning: 108 : BindingExpression (hash=14976165): At level 0 - for WaferMap.ActualSize found accessor DependencyProperty(ActualSize) System.Windows.Data Warning: 104 : BindingExpression (hash=14976165): Replace item at level 0 with WaferMap (hash=14353717), using accessor DependencyProperty(ActualSize) System.Windows.Data Warning: 101 : BindingExpression (hash=14976165): GetValue at level 0 from WaferMap (hash=14353717) using DependencyProperty(ActualSize): Size (hash=0) System.Windows.Data Warning: 80 : BindingExpression (hash=14976165): TransferValue - got raw value Size (hash=0) System.Windows.Data Warning: 89 : BindingExpression (hash=14976165): TransferValue - using final value Size (hash=0) WaferMap.ActualSize.get的更改事件。)

谢谢!

1 个答案:

答案 0 :(得分:1)

您也许可以使用如下所示的简单派生形状。

它绘制一个ArcSegment,在底部中心有一个凹口,您要做的就是调整凹口的大小。

public class WaferMap : Shape
{
    private readonly StreamGeometry geometry = new StreamGeometry();

    protected override Geometry DefiningGeometry => geometry;

    protected override Size MeasureOverride(Size size)
    {
        if (double.IsInfinity(size.Width))
        {
            size.Width = StrokeThickness;
        }

        if (double.IsInfinity(size.Height))
        {
            size.Height = StrokeThickness;
        }

        return size;
    }

    protected override Size ArrangeOverride(Size size)
    {
        var x = size.Width / 2;
        var y = size.Height - StrokeThickness / 2;
        var arcSize = new Size(
            (size.Width - StrokeThickness) / 2, (size.Height - StrokeThickness) / 2);

        using (var sgc = geometry.Open())
        {
            sgc.BeginFigure(new Point(x, y - 20), true, true);
            sgc.LineTo(new Point(x - 10, y), true, true);
            sgc.ArcTo(new Point(x + 10, y), arcSize, 0, true,
                      SweepDirection.Clockwise, true, true);
        }

        return size;
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        drawingContext.DrawGeometry(Fill, new Pen(Stroke, StrokeThickness), geometry);
    }
}

您将像使用其他任何Shape元素一样使用它:

<local:WaferMap Stroke="Orange" StrokeThickness="3" Margin="10"/>
相关问题