簡體   English   中英

當 Prism 中的另一個屬性發生變化時更新一個屬性

[英]Update a property when another property changes in Prism

我的視圖模型中有類似的東西

        public ObservableCollection<string> UnitSystems { get; set; }
        private string selectedUnitSystem;
        public string SelectedUnitSystem
        {
            get { return selectedUnitSystem; }
            set { SetProperty(ref selectedUnitSystem, value); }
        }

        private string _property1Unit;
        public string Property1Unit
        {
            get { return _property1Unit; }
            set { SetProperty(ref _property1Unit, value); }
        }

在我看來,它們綁定到組合框和標簽。 當我在組合框中選擇其他內容時,我當然想更新值 Property1Unit 和標簽的內容。 是否可以?

對的,這是可能的。 您可以在設置的值之后傳入您想要執行的任何操作。 下面的示例顯示了一種情況,其中您有一個值連接 FirstName 和 LastName 的兩個字符串。 這只會在屬性更改時執行,因此如果值是Dan並且新值是Dan它將不會執行,因為它首先不會引發 PropertyChanged。

public class ViewAViewModel : BindableBase
{
    private string _firstName;
    public string FirstName
    {
        get => _firstName;
        set => SetProperty(ref _firstName, value, () => RaisePropertyChanged(nameof(FullName));
    }
    private string _lastName;
    public string LastName
    {
        get => _lastName;
        set => SetProperty(ref _lastName, value, () => RaisePropertyChanged(nameof(FullName));
    }

    public string FullName => $"{FirstName} {LastName}";
}

暫無
暫無

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

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