Devexpress RaisePropertyChanged


  • 所有的重載設置字段作為參數傳遞到指定的值,而屬性提高INotifyPropertyChanged.PropertyChanged事件。

  • 如果一個字段已經成功地改變,setProperty方法中返回一些重載也需要一個回調方法作為參數。調用該回調后場已經改變。

  • 手動設置一個基本的私有字段,然后調用的BindableBase.RaisePropertyChanged方法重載之一

    所有RaisePropertyChanged方法重載需要作為輸入參數,這些屬性提高INotifyPropertyChanged.PropertyChanged事件。

 

using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Xpf.Mvvm;

public class BindableObject : BindableBase {

    //The property's setter below uses the SetProperty method, which does the following: 
    //1. Changes the underlying private field. 
    //2. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty. 
    //This is the preferable method for setting a property in most cases, because it provides strong typing.  
    //The method is slightly slower than passing the property name as a string (see the declaration of StringProperty2). 
    string stringProperty;
    public string StringProperty {
        get { return stringProperty; }
        set { SetProperty(ref stringProperty, value, () => StringProperty); }
    }

    //The property's setter below uses the SetProperty method, which does the following: 
    //1. Changes the underlying private field. 
    //2. Raises a change notification for the property with the specified name. 
    //This method is slightly  faster than the previous variant, but does not provide strong typing. 
    string stringProperty2;
    public string StringProperty2 {
        get { return stringProperty2; }
        set { SetProperty(ref stringProperty2, value, "StringProperty2"); }
    }

    //The property's setter below uses the SetProperty method, which does the following: 
    //1. Changes the underlying private field. 
    //2. Invokes your callback method (OnStringProperty3Changed) if the property has been changed. 
    //3. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty3. 
    string stringProperty3;
    public string StringProperty3 {
        get { return stringProperty3; }
        set {
            SetProperty(ref stringProperty3, value, () => StringProperty3, OnStringProperty3Changed);
            //An alternative form: 
            SetProperty(ref stringProperty3, value, () => StringProperty3, () => OnStringProperty3Changed());
        }
    }
    private void OnStringProperty3Changed() {
    }

    //The property's setter below uses the SetProperty method, which does the following: 
    //1. Changes the underlying private field. 
    //2. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty4. 
    //You can write additional code within the "if" statement, which will be executed after the property has changed. 
    string stringProperty4;
    public string StringProperty4 {
        get { return stringProperty4; }
        set {
            if (SetProperty(ref stringProperty4, value, () => StringProperty4)) {
                //The SetProperty method returns true if the property has been changed. 
                ///Do “something” after the property has changed.  
                //... 
            }
        }
    }

    //The following property's setter does the following: 
    //1. Changes the underlying private field. 
    //2. Raises a change notification via the RaisePropertyChanged method. 
    string stringProperty5;
    public string StringProperty5 {
        get { return stringProperty5; }
        set {
            if (stringProperty5 == value)
                return;
            stringProperty5 = value;
            RaisePropertyChanged("StringProperty5");
            //An alternative form using a lambda expression, which provides strong typing: 
            RaisePropertyChanged(() => StringProperty5);
            //Do “something” after the property has changed.  
            //... 
        }
    }

    //The following property's setter changes a property, and raises change notifications for this property and its dependent properties: 
    //1. Changes the underlying private field (stringProperty6). 
    //2. Raises change notifications for StringProperty and StringProperty2. 
    //3. Raises a change notification for StringProperty6. 
    //This is the preferable variant in most cases, because it features strong typing. 
    //The method is slightly slower than passing the property name as a string (see the implementation of StringProperty7). 
    string stringProperty6;
    public string StringProperty6 {
        get { return stringProperty6; }
        set {
            SetProperty(ref stringProperty6, value, () => StringProperty6, () => RaisePropertiesChanged(() => StringProperty, () => StringProperty2));

            //An alternative form that is easier to read: 
            if (SetProperty(ref stringProperty6, value, () => StringProperty6)) {
                RaisePropertiesChanged(() => StringProperty, () => StringProperty2);
            }
        }
    }

    //The following property's setter changes the property and raises change notifications for this property and its dependent properties: 
    //1. Changes the underlying private field (stringProperty7). 
    //2. Raises a change notification for StringProperty7. 
    //3. Raises change notifications for StringProperty and StringProperty2. 
    //This is a quicker alternative to the previous example, but does not provide strong typing. 
    string stringProperty7;
    public string StringProperty7 {
        get { return stringProperty7; }
        set {
            if (SetProperty(ref stringProperty7, value, () => StringProperty7)) {
                RaisePropertiesChanged("StringProperty", "StringProperty2");
            }
        }
    }
}

該代碼來自官網


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM