簡體   English   中英

屬性更改的Template10類

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM