CustomControl XAML: ArcSegment.Size 绑定到控件的实际宽度和高度
CustomControl XAML: ArcSegment.Size binding to control's Actual Width and Height
我的程序员们!
我已经在网上搜索过,没有找到解决办法。可能这是因为我是 WPF 的新手。我正在努力实现以下目标:
CustomControl
有一个 ArcSegment
,其 Size
绑定到 CustomControl
的大小:
这应该说明带有管芯的晶圆(半导体行业)
所以我明白我需要在 XAML 中使用 <Path>
来构造 'circle with notch'
我的问题是无法将 ArcSegment
元素绑定到 CustomControl 的 Size
。 ArcSegment
有 Size
属性,CustomControl
有 ActualWidth
和 ActualHeight
。我尝试了几种方法,但都不适合我。
这是我的 class 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)));
}
}
以及我使用的 XAML 样式:
<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
,但首先我想看看这个简单的东西对我有用。不幸的是,事实并非如此。我在我的输出中看到了这个 window:
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
永远不会被调用。 setter 在我更改表单大小时被调用(如您所见,我已注册更改 ActualWidth
和 ActualHeight
的事件)。
谢谢!
您或许可以使用如下所示的简单派生形状。
它绘制了一个弧线段,在底部中心有一个缺口,您只需调整缺口的大小即可。
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);
}
}
您可以像使用任何其他形状元素一样使用它:
<local:WaferMap Stroke="Orange" StrokeThickness="3" Margin="10"/>
我的程序员们!
我已经在网上搜索过,没有找到解决办法。可能这是因为我是 WPF 的新手。我正在努力实现以下目标:
CustomControl
有一个 ArcSegment
,其 Size
绑定到 CustomControl
的大小:
这应该说明带有管芯的晶圆(半导体行业)
所以我明白我需要在 XAML 中使用 <Path>
来构造 'circle with notch'
我的问题是无法将 ArcSegment
元素绑定到 CustomControl 的 Size
。 ArcSegment
有 Size
属性,CustomControl
有 ActualWidth
和 ActualHeight
。我尝试了几种方法,但都不适合我。
这是我的 class 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)));
}
}
以及我使用的 XAML 样式:
<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
,但首先我想看看这个简单的东西对我有用。不幸的是,事实并非如此。我在我的输出中看到了这个 window:
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
永远不会被调用。 setter 在我更改表单大小时被调用(如您所见,我已注册更改 ActualWidth
和 ActualHeight
的事件)。
谢谢!
您或许可以使用如下所示的简单派生形状。
它绘制了一个弧线段,在底部中心有一个缺口,您只需调整缺口的大小即可。
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);
}
}
您可以像使用任何其他形状元素一样使用它:
<local:WaferMap Stroke="Orange" StrokeThickness="3" Margin="10"/>