我目前正在设计一个系统(我在 WPF 中的第一个),我希望将我的自定义控件的属性绑定到控件样式中设置的元素。
<Style TargetType="{x:Type c:Connection}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:Connection}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Line Stroke="Red" X1="90" X2="90" Y1="90" Y2="5"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我希望将 Line (X1/2 Y1/2) 的元素绑定到我的连接控件中的属性。但是,只要我添加一个连接元素(在代码中,即使没有绑定),我就会得到一个类型初始化错误。我的连接类如下:
public class Connection : Control
{
public Connector StartElement { get; set; }
public Connector EndElement { get; set; }
#region Properties
#endregion
}
我初始化如下: Connection con = new Connection(); (然后我将它添加到画布上)。
如何将坐标绑定到连接器中的点?(例如 StartElement.GetXPosition());
亲切的问候汤姆