1

in my C# WPF Project (working with VS2012) my goal is to use existing Data from a class in a new Window I created...

Therefore I added a new Window (WPF) to my Project and called it DijkstraWindow. In my MainWindow there is a Menu and when you click the suitable item the DijkstraWindow is opened. In my MainWindow.xaml.cs this is the Code to do this:

private void Dijkstra_Click(object sender, RoutedEventArgs e)
{
     var DWindow = new DijkstraWindow();
     DWindow.Show();
}

Now I need to access data (which is created while the application is running) and this is stored in a list which is stored in a class. But I have no idea how to do this.

I tried the following:

1.

Creating a new object in DijkstraWindow:

var mwvm = new MainWindowViewModel();

The data is accessible (in my new DijkstraWindow) but it just takes the data which is initialized when starting the application. So this is the wrong way. Because there a some list which is filled while the application is running. I want to use this data in my new Window.

2.

In my DijkstraWindow.xaml.cs I tried to inherit from the class where my data is, but then the compiler is complaining

"Partial declarations must not specify different base classes"

So I read you have also to changed your xaml file, so changed it to:

<local:MainWindowViewModel x:Class="Graphomat.DijkstraWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Graphomat"
    Title="DijkstraWindow" Height="300" Width="300">
    <Grid/>
</local:MainWindowViewModel>

This is also not working, then my DijkstraWindow has no information about the show method?

Could someone please help me out with this?

Thank you!

edit

Here ist the Class Declaration:

*/using somestuff */

namespace Graphomat
{
/// <summary>
/// Interaction logic for DijkstraWindow.xaml
/// </summary>
public partial class DijkstraWindow : MainWindowViewModel
{
    public DijkstraWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }

  }

}

Tried to inherit from class "MainWindowViewModel", but this doesn't work because the xaml file..

mho
  • 53
  • 1
  • 1
  • 6

1 Answers1

1

The compiler is complaining because your view's type Graphomat.DijkstraWindow doesn't declare the same base type between the xaml and the .cs file. Your cs file likely says that it inherits from the Window type.

One way to transfer data between ViewModels is dependency injection. Consider the following:

public class FooView : Window
{   
    //require data from the parentview to the child view through dependency injection.  
    //very simplistic, might meet your needs.  If you need a full view lifecycle, see MVVM frameworks like
    //cliburn.micro
    public FooView(INavigationData navigationData)
    {
        //do something with your data.
    }
}

It is very common to use a base class for all the view models in you project. Consider that you are binding all your views to individual view models, it only makes sense that you would create a base implementation of INotifyPropertyChanged on a base class:

public class MainViewModel : BaseViewModel
{

}

public abstract class BaseViewModel : INotifyPropertyChanged
{
    public object Model { get; set; }

    #region PropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if(handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region Commands

    public ICommand OpenFooWindowClicked
    {
        get
        {
            //implement your ICommand here... beyond the scope of the question.
        }
    }

    #endregion
}

As far as the class problem is concerned, if you are following the typical MVVM naming convention, then it looks like you're trying to define your ViewModel in xaml. While that's not unheard of, you likely want to define your View in xaml.

Please do check out the SO question: MVVM: Tutorial from start to finish? The tutorials linked in that thread should get your head wrapped around the concepts vital to successful execution of the MVVM pattern.

Community
  • 1
  • 1
Todd Richardson
  • 1,119
  • 1
  • 11
  • 22
  • Thank you for your detailed comment. Thought it would easier to do this. While reading the tutorials it seems to be necessary to restructur my complete application. At the moment I'm not sure what my View, Model or ModelView is, my code is pretty mixed up with everything ... – mho Jan 20 '14 at 13:41