【问题标题】:How to get all the View(UserControl) in the ViewModel using MVVM Light?如何使用 MVVM Light 获取 ViewModel 中的所有 View(UserControl)?
【发布时间】:2013-05-31 01:28:41
【问题描述】:

我正在使用 MVVM Light,但我不知道如何在 ViewModel 中获取所有视图,如果我使用架构 ViewModel => View 是可能的。

public MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        View = new AView();
    }

    public UIElement View { get; set; }
}

<UserControl x:Class="AView">
</UserControl>

但现在我无法切换。

我想导出(EXCEL、PDF 等)所有 UserControl(视图),现在在 MVVMLight 我有 View => ViewModel:

<UserControl
    DataContext={Binding MyViewModel, Source=StaticResource Locator} >
</UserControl>

非常感谢。

【问题讨论】:

    标签: silverlight mvvm mvvm-light silverlight-5.0


    【解决方案1】:

    您可以在后面的视图代码中设置View 属性,如下所示:

    public MyView : UserControl
    {
        public MyViewModel ViewModel { get { return (this.DataContext as MyViewModel); } }
        public MyView()
        {
            InitializeComponent();
            this.Loaded += onLoaded;
        }
        void onLoaded(object sender, RoutedEventArgs e)
        {
            ViewModel.View = this;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多