【问题标题】:Cannot bind to Dependency Property of type Point[]无法绑定到 Point[] 类型的依赖属性
【发布时间】:2016-03-10 16:51:07
【问题描述】:

我在用户控件后面的代码中定义了一个依赖属性,如下所示:

public partial class StockControl : UserControl
{
    public static readonly DependencyProperty DataProperty;

    static StockControl()
    {
        FrameworkPropertyMetadata md = new FrameworkPropertyMetadata(DataPropertyChanged);
        DataProperty = DependencyProperty.Register("Data", typeof(Point[]), typeof(StockControl), md);
    }    

    public StockControl()
    {
        InitializeComponent();
    }

    private static void DataPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {

    }

    public Point[] Data
    {
        get { return (Point[])GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }
}

我正在尝试将整个点数组绑定到我的 XAML 中的此依赖项属性。但是,数据属性未显示在智能感知中。如果我将数据类型从 Point[] 切换到 Point 类型的列表集合,那么它可以工作。

我不想使用集合。有没有办法可以使用原始 Point[] 类型来完成这项工作?我读过 Properties 不应该返回数组,因为它们是可变的,并且可能被篡改。这就是它不起作用的原因吗?

【问题讨论】:

  • 你说它没有出现在智能中,但它会构建吗?我不会像往常一样认为 xaml 智能是正确的。

标签: c# binding dependency-properties


【解决方案1】:

我已经尝试过你的代码,它为我构建了一个点数组 dp 很好。

<Window.Resources>
    <x:Array Type="Point" x:Key="myPoints">
         <Point X="50" Y="50"></Point>
    </x:Array>
 </Window.Resources>


<local:StockControl Data="{StaticResource myPoints}">

</local:StockControl>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2016-01-22
    • 2018-09-20
    • 2023-03-26
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多