【问题标题】:Propertychanged Template10 ClassPropertychanged Template10 类
【发布时间】:2016-06-21 01:06:14
【问题描述】:

使用 Template10 的 UWP 应用的 viewmodel 中的 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); } }

【问题讨论】:

    标签: c# uwp template10


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      创建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 sn-p 最容易创建它们。

      我建议给this MVA course 看看(尤其是第一课)如何创建自定义控件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        相关资源
        最近更新 更多