Ono
Ono

Reputation: 1357

Access ViewModel of a wpf dll

I have a wpf MultiROIStats.dll with mode, view, ViewModel. Here is the C# of the view:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MultiROIStats
{

    using ViewModel;

    //xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    /// <summary>
    /// Interaction logic for MultiROIStats.xaml
    /// </summary>
    public partial class MultiROIStats : Window
    {
        public MultiROIStats()
        {          
            InitializeComponent();
            DataContext = new MultiROIStatsViewModel();
        }
    }
}

To use this MultiROIStats,dll, I insert it info the reference of another project. Now I need to access the ViewModel (some methods there) of the inserted MultiROIStats.dll. I am wondering how should I do this? I initiated an object of the inserted MultiROIStats.dll, but cannot find the method I want to use in its ViewModel:

 private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            MultiROIStats mroi = new MultiROIStats();

            mroi.Show();   

           // here should be mroi.viewmode.dothings() ... but I don't know how to access it

        }

Upvotes: 1

Views: 347

Answers (2)

Fede
Fede

Reputation: 44028

var window = new MultiROIStats();
window.Show();   

var vm = window.DataConntext as MultiROIStatsViewModel;
vm.DoThings();

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102723

You should be able to get to it by casting the DataContext to the view-model type:

MultiROIStats mroi = new MultiROIStats();
mroi.Show();   

var viewmodel = mroi.DataContext as MultiROIStatsViewModel;
if (viewmodel != null)
    viewmodel.dothings();

Upvotes: 1

Related Questions