【问题标题】:WPF binding with custom controlWPF 与自定义控件绑定
【发布时间】:2013-03-15 04:53:30
【问题描述】:

我目前正在设计一个系统(我在 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) 的元素绑定到我的连接控件中的属性。但是,只要我添加一个连接元素(在代码中,即使没有绑定),我就会得到一个类型初始化错误。我的 Connection 类如下:

public class Connection : Control
{
    public Connector StartElement { get; set; }
    public Connector EndElement { get; set; }

   #region Properties


    #endregion

}

我初始化如下: Connection con = new Connection(); (然后我将它添加到画布上)。

如何将坐标绑定到连接器中的点? (ex StartElement.GetXPosition());

亲切的问候 汤姆

【问题讨论】:

  • 这听起来像是我在this example做的事情
  • 您得到的确切错误是什么,您的代码中的哪个位置导致了错误?如果您包含初始化代码会有所帮助。

标签: wpf binding properties


【解决方案1】:

您确定您的元素创建正确吗?

这对我有用:

在Connection.cs 文件中

using System.Windows.Controls;

public class Connector {
  public int X { get; set; }
  public int Y { get; set; }
}

public class Connection : Control {
  public Connector StartElement { get; set; }
  public Connector EndElement { get; set; }
}

Xaml:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1">
  <UserControl.Resources>
    <Style TargetType="{x:Type local:Connection}">
      <Setter Property="SnapsToDevicePixels"
              Value="true" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type local:Connection}">
            <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
              <Line Stroke="Red"
                    X1="{Binding StartElement.X}"
                    X2="{Binding EndElement.X}"
                    Y1="{Binding StartElement.Y}"
                    Y2="{Binding EndElement.Y}" />
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </UserControl.Resources>
  <Canvas x:Name="canvasElement"
          Background="White" />
</UserControl>

和UserControl 代码隐藏:

public UserControl1() {
  InitializeComponent();
  Connection connectionVariable = new Connection {
    StartElement = new Connector { X = 0, Y = 0 },
    EndElement = new Connector { X = 300, Y = 300 }
  };
  canvasElement.Children.Add(connectionVariable);
  Canvas.SetLeft(connectionVariable, 0);
  Canvas.SetTop(connectionVariable, 0);
}

运行这个,我看到一条红色的对角线。

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 2011-08-06
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多