【问题标题】:How do I set a property in a ViewModel from another ViewModel in MVVM Light Toolkit [duplicate]如何在 MVVM Light Toolkit 中的另一个 ViewModel 中设置 ViewModel 中的属性 [重复]
【发布时间】:2015-06-11 13:17:58
【问题描述】:

我有一个包含几个 TextBox 元素和一个进度条的表单。我希望在 TextBox 分配了一些值时更新进度条。

因此,当设置属性时,我尝试增加 ProgressPercent,但实际上不起作用。我错过了什么,为什么我不能从另一个 ViewModel 访问 ProgressPercent?

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    private int progressPercent { get; set; }

    public MainViewModel()
    {
    }

    public int ProgressPercent
    {
        get
        {
            return this.progressPercent;
        }
        set
        {
            this.progressPercent = value;
            this.RaisePropertyChanged(() => this.ProgressPercent);
        }
    }
}

FooterViewModel

public class FooterViewModel : ViewModelBase
{
    private string firstName { get; set; }
    private string lastName { get; set; }

    public FooterViewModel()
    {
    }

    public string FirstName
    {
        get
        {
            return this.firstName;
        }
        set
        {
            this.firstName = value;
            this.RaisePropertyChanged(() => this.FirstName);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }

    public string LastName
    {
        get
        {
            return this.lastName;
        }
        set
        {
            this.lastName = value;
            this.RaisePropertyChanged(() => this.LastName);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }
}

HeaderViewModel.cs

public class HeaderViewModel : ViewModelBase
{
    private string address { get; set; }
    private int age { get; set; }

    public HeaderViewModel()
    {
    }

    public string Address
    {
        get
        {
            return this.address;
        }
        set
        {
            this.address = value;
            this.RaisePropertyChanged(() => this.Address);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }

    public int Age
    {
        get
        {
            return this.age;
        }
        set
        {
            this.age = value;
            this.RaisePropertyChanged(() => this.Age);
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; 
            });
        }
    }
}

Accessing a property in one ViewModel from another 的问题没有什么不同,但由于某种原因,那里的解决方案对我不起作用

其实答案就在这里 Accessing Properties in other ViewModels in MVVM Light

【问题讨论】:

  • 遇到了类似的问题,尽管我使用的是 caliburn.micro,但建议的解决方案也应该对您有所帮助 - 将第二个视图模型注册到第一个的 propertychange 事件并更新您的任何内容想要link

标签: c# wpf mvvm viewmodel


【解决方案1】:

您通常必须建立某种类型的通信基础设施才能在 ViewModel 之间进行通信。您是否使用任何类型的 Mediator 或 MVVM 框架,该框架具有内置的脚手架?

或者,您可以在一个类中设置一个属性,所有这些属性都可以访问,这可能引发其他 ViewModel 订阅的事件。

【讨论】:

  • 我正在使用 mvvm light 工具包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多