0

使用 Template10 的 UWP 应用的视图模型中的 propertychanged 触发器通过以下方式触发:

public var Thing{ get { return thing; } set { Set(ref thing, value); } }

Set 函数放在类 bindableBase 中。

如何在用户控件中使用相同的功能?

我尝试了以下方法,但没有奏效:

BindableBase x;

var foo;
public var Foo{ get { return foo; } set { x.Set(ref foo, value); } }
4

2 回答 2

0

例如,如果您放置用户控件的页面将具有与填充绑定到页面的 DataContext 的视图模型的用户控件部分的字段相关联的属性,则您不会以与视图模型一起使用的方式使用。我认为您需要查看 MVVM。或者视图模型可能是相关 userControl 的 DataContext。

于 2016-06-18T04:20:39.950 回答
0

创建 a 时UserControl,您需要使用 aDependencyProperty创建可绑定属性。Page在其他控件(如 a )中使用 UserControl 时,需要使它们按预期运行。DependencyProperties 定义如下:

    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

propdp使用Visual Studio 中的代码段最容易创建它们。

我建议看看这门 MVA 课程(尤其是第一课)如何创建自定义控件

于 2016-06-20T17:06:14.287 回答