UWP XAML 用户控件静态属性与实例属性

UWP XAML User control static properties vs instance properties

所以我创建了一个用户控件,并在其上创建了一个依赖项 属性 TestProperty。

我想要此用户控件的 2 个实例,并将 TestProperty 设置为每个实例的不同值。

<widgets:mycontrol Test="val1"></widgets:WTComboBox>
<widgets:mycontrol Test="val2"></widgets:WTComboBox>

如果我创建 test属性 作为静态 DependencyProperty,如下所示:

 public static readonly DependencyProperty TestProperty=
        DependencyProperty.Register("Test", typeof(string), typeof(mycontrol), null);

在控件中,那么显然我不能在每个实例中为它设置不同的值,但是当我将 test属性 创建为普通实例时 属性

public DependencyProperty TestProperty;

public mycontrol()
{
    TestProperty = DependencyProperty.Register("Test", typeof(string), typeof(mycontrol), null);
}

在控件上,然后设计者无法识别 属性 的存在并产生错误,但在运行时控件工作正常并且控件的每个实例都具有正确的测试值 属性.

所以问题是如何让实例依赖性属性在设计器中工作?

谢谢

DependencyProperty 注册到 Type,而不是实例。您正确编写此代码以注册它。 Reference from here.

public static readonly DependencyProperty TestProperty=
    DependencyProperty.Register("Test", typeof(string), typeof(mycontrol), null);

然后你说,

then obviously I can't have a different value for it in each instance

这不是真的。

您可以为每个实例设置不同的值,事件虽然 DependencyProperty 是以这种方式注册的,但它不是 static 属性 可以该类型的所有实例只有一个值,就像您可能已经排除了常规 CLR 属性。要了解 Dependency 属性 和 CLR 属性 之间的差异,请参阅 this answer

然后你定义一个non-static依赖属性(请问如何让设计者识别这个属性),你不应该这样做.已经有 some answer 为什么你不应该写一个非静态依赖 属性 即使它看起来可以工作。