[英]Propertychanged Template10 Class
通过以下方式触发具有Template10的UWP应用的视图模型中的属性更改触发器:
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); } }
您不能以这种方式使用视图模型,例如,如果放置用户控件的页面将具有与填充绑定到页面的DataContext的视图模型的usercontrol部分的字段相关的属性。 我认为您需要查看MVVM。 或者,视图模型可以是所讨论的userControl的DataContext。
创建UserControl
您需要使用DependencyProperty
创建可绑定的属性。 在其他控件(如Page
)中使用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));
使用Visual Studio中的propdp
代码片段最容易创建它们。
我建议让该MVA课程 (特别是第一课)介绍如何创建自定义控件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.