【问题标题】:Catel MVVM: How to pass data between windowsCatel MVVM:如何在窗口之间传递数据
【发布时间】:2014-08-28 11:27:05
【问题描述】:

大家好,我正在努力解决在 Catel MVVM 模型中的视图模型之间发送数据的过程。我有一个按钮,单击它我想打开一个新窗口并将一些数据(对象)发送到新打开的窗口。但是我自己无法解决这个问题,你能帮帮我吗?

在我的第一个视图模型中:

private readonly IShowStopInfo stopInfo;

//Main constructor of the class
public StopViewModel(IGrtrService grtrService, IShowStopInfo stopInfo)
{
    this.stopInfo = stopInfo;

    Argument.IsNotNull(() => grtrService);
    _grtrService = grtrService;

    AllStops = _grtrService.LoadStop();
    Stop_Line = _grtrService.LoadLines();

    ShowSelectedValue = new Command(OnStopsInfo);
}
public Command ShowSelectedValue { get; private set; }

private void OnStopsInfo()
{
    stopInfo.ShowStopInfo();
}
//Getting Selected Stop from the list
public Stop SelectedStop
{
    get { return GetValue<Stop>(SelectedStopProperty); }
    set { SetValue(SelectedStopProperty, value); }
}
public static readonly PropertyData SelectedStopProperty =  RegisterProperty("SelectedStop", typeof(Stop));

在我的情况下,我想从“SelectedStop”方法发送结果,我该怎么做?

【问题讨论】:

  • 请务必标记之前问题的答案,例如stackoverflow.com/questions/25280047/…
  • 这如何回答我的问题?
  • 它有助于让人们有动力回答你的问题,所以这样肯定会有所帮助;-)

标签: c# wpf mvvm catel


【解决方案1】:

请查看 Catel 的“Getting started with WPF”部分。它将指导您创建具有所有基础知识的第一个应用程序。基础之一是显示带有上下文的窗口。

你可以找到非常详细的解释(有截图等)here

基本上,您必须使用窗口视图模型的第一个参数作为模型注入(在示例中是 Person 模型)。

然后查看“hooking up everything together”部分,尤其是命令(其中 IUIVisualizerService 用于创建包含当前选定的人或家庭的窗口)。

例如,详细 vm 如下所示:

public class MyStopDetailsViewModel : ViewModelBase
{
    public MyStopDetailsViewModel(Stop myStop, IMessageService messageService, IPleaseWaitService pleaseWaitService)
    {
         // Here is the context with the injected Stop parameter. Note that I have
         // also injected several other services to show how you can use the combination
    }
}

您可以按如下方式调用它(如示例中所示):

var typeFactory = this.GetTypeFactory();
var stopDetailsViewModel = typeFactory.CreateInstanceWithParametersAndAutoCompletion<MyStopDetailsViewModel>(SelecteedStop);
_uiVisualizerService.ShowDialog(stopDetailsViewModel );

【讨论】:

  • hmmm 但在我的情况下 public Stop SelectedStop 是一个构造函数我如何传递它的值?
  • public class SelectedStopViewModel { public SelectedStopViewModel(Stop stopModel, IAnotherDependency anotherDependency) { // 这里是你的上下文 } }
猜你喜欢
  • 2011-03-11
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多