簡體   English   中英

觸發OnPropertyChanged時更新其他屬性

[英]Update other properties when OnPropertyChanged was triggered

我在這里檢查了其他鏈接,但無法真正找到正確的答案。 我有xaml,其中包含兩個文本框。 第一個是小時,第二個是分鍾。 每當我在文本框中更改小時值時,分鍾應重置為0。如何使用OnPropertyChange

public class Variable : INotifyPropertyChanged
{
    public Variable()
    {
        this.hours = "1";
        this.minutes = "2";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private string hours;
    private string minutes;

    public string Hours
    {
        get { return this.hours.ToString(); }
        set
        {
            if (this.hours != value)
            { 
                this.hours = value;
                this.minutes = "0";
                this.OnPropertyChanged("Hours");
            }
        }
    }

    public string Minutes { get { return this.minutes; } }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName ));
    }
}

除非Minutes文本框是只讀的,否則您應該為該屬性設置一個setter:

public string Minutes
{
    get => this.minutes; 
    set
    {
        if (this.minutes != value)
        {
            this.minutes = value;
            OnPropertyChanged();
        }
    }
}

現在,您可以在“ Hours設置器中使用此設置器來通知UI更改:

if (this.hours != value)
{ 
    this.hours = value;
    this.OnPropertyChanged();
    this.Minutes = "0";
}

相反,如果Minutes是正確的只讀屬性,那么您有兩個選擇:將其setter設為private (並按上述方式使用)或手動調用OnPropertyChanged()通知UI更改:

if (this.hours != value)
{ 
    this.hours = value;
    this.OnPropertyChanged();

    this.minutes = "0";
    this.OnPropertyChanged(nameof(Minutes));
}

我強烈反對第二種選擇,因為它增加了不必要的復雜性,除非絕對需要,否則我不想手動通知更改。


所有這些都說明您可以在代碼中再改善幾處。

OnPropertyChanged()具有帶有[CallerMemberName]屬性的參數,則您無需指定屬性名稱(如果從該屬性內部調用的話)。 必要時(請參見第二個示例),請使用nameof(PropertyName)而不是"PropertyName"因為在重命名屬性時它將自動更改。

我對您的代碼不太了解 ,但是如果HoursMinutes是integer屬性,那么您應該使用int而不是string 如果輸入錯誤,則最好盡快通知用戶。 另外,您應該驗證值:

if (value < 0 || value >= 60)
    throw new ArgumentOutOfRangeException(...);

並非在這種情況下,但通常是當您擁有一個僅在沒有后綴的情況下才有后備字段的屬性時,可以使用:

public string Minutes { get; private set; }

只需再次使用名稱Minutes調用OnPropertyChanged調用程序即可

OnPropertyChanged(nameof(Minutes));

或者,您可以將私有設置程序添加到在其中調用它的minutes屬性。 因此,您將獲得以下內容:

public class Variable : INotifyPropertyChanged
{
    public Variable()
    {
        this.hours = "1";
        this.minutes = "2";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private string hours;
    private string minutes;

    public string Hours
    {
        get { return this.hours.ToString(); }
        set
        {
            if (this.hours != value)
            { 
                this.hours = value;
                this.OnPropertyChanged();

                this.Minutes = "0";                    
            }
        }
    }

    public string Minutes 
    { 
        get { return this.minutes; }
        private set
        {
            if(this.minutes == value)
                return;

            this.minutes = value;
            OnPropertyChanged()
        }
    }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName ));
    }
}

順便說一句,調用者中的[CallerMemberName]屬性意味着,如果您不傳遞任何參數值,它將使用調用它的人的名字。 如果是屬性,它將是該屬性的名稱,因此您不必編寫它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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