michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

MVVMCViewModel SubClass #12

Closed mark-edward-theaccessgroup-com closed 4 years ago

mark-edward-theaccessgroup-com commented 4 years ago

Thanks very much for the useful framework, it's really helped with knocking up a quick wizard app in WPF, which I was a bit rusty with.

I wanted to subclass MVVMCViewModel, to store a few common properties used in my individual wizard views, but when I did this, the convention for allocating viewmodel to view fails in the MVVMCNavigationService.NavigateWithController<TViewModel>(object parameter) where TViewModel : MVVMCViewModel method.

Could this be adapted to search for the correct MVVMCViewModel base class of the TViewModel passed in? I'd be happy to have a go at it, using something along these lines if you think it's not a terrible idea?

Thanks, Mark

michaelscodingspot commented 4 years ago

Hi, I was actually able to create a base class that derives from MVVCViewModel, and to derive from it successfully.

public class MyViewModel123 : MVVMCViewModel
{  
}

public class InitialViewModel : MyViewModel123
{
}
// ... 
// The following works:
NavigationService.NavigateWithController<MainOperation.InitialViewModel>();

I created a branch that showcases the scenario "derivingFromMVVMCViewModel".

  1. Clone
  2. Move to branch "derivingFromMVVMCViewModel".
  3. Open in VS and set as startup project "MainApp"
  4. Place breakpoint in AddWizardController.cs line 24
  5. Run application
  6. Click "Add Employee"
  7. Click "Cancel"

Is this what you meant?

mark-edward-theaccessgroup-com commented 4 years ago

Thanks for getting back to me Michael. I've got a horrible feeling a some of the problems in my project may be namespace related, but I've set up the a sub class in your MainApp project too, and am having problems binding from properties on it to the UserControl.

Like I said earlier, I am coming back to WPF after several years away from it, so am probably be doing something stupid, but after adding properties to the InitialViewModel and the MyViewModel123 base class I failed to get them to bind to the view properly, with both of the TextBlocks showing nothing at all.

Many thanks again for looking into this.

InitialViewModel.cs

 public class MyViewModel123 : MVVMCViewModel
    {
        private string _testPropBase = "TEST PROP VAL BASE";

        public string TestPropertyBase
        {
            get { return _testPropBase; }
            set { _testPropBase = value; OnPropertyChanged("TestPropertyBase"); }
        }
    }

    public class InitialViewModel : MyViewModel123
    {
        private string _testProp = "TEST PROP VAL";

        public string TestProperty
        {
            get { return _testProp; }
            set { _testProp = value; OnPropertyChanged("TestProperty"); }
        }

    }

InitialView.xaml


<UserControl x:Class="MainApp.Pages.MainOperation.InitialView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MainApp.Pages.MainOperation"
             xmlns:mvvmc="clr-namespace:MVVMC;assembly=MVVMC"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:InitialViewModel/>
    </UserControl.DataContext>
    <Grid>
        <StackPanel VerticalAlignment="Top" Margin="20">
            <TextBlock Text="{Binding Path=TestProperty}" Background="Red"></TextBlock>
            <TextBlock Text="{Binding Path=TestPropertyBase}" Background="Yellow"></TextBlock>
            <TextBlock HorizontalAlignment="Center" >Select an Operation:</TextBlock>
            <Button Margin="10" Width="150" Command="{mvvmc:NavigateCommand ControllerID='MainOperation', Action='AllEmployees'}">View Employees</Button>
            <Button Margin="10" Width="150" Command="{mvvmc:NavigateCommand ControllerID='MainOperation', Action='AddEmployee'}">Add Employee</Button>
            <Button Margin="10" Width="150" Command="{mvvmc:NavigateCommand ControllerID='MainOperation', Action='About'}">About</Button>
        </StackPanel>
    </Grid>
</UserControl>
michaelscodingspot commented 4 years ago

Ok, I was able to reproduce the issue and you are right, it really doesn't work when not deriving directly from MVVMCViewModel.

I agree it's a correct behavior to allow it so I did the change in this commit. You can now download the latest NuGet 1.1.8 and this should work for you.

Please try it and close the issue if everything works well.

Michael

mark-edward-theaccessgroup-com commented 4 years ago

That's brilliant Michael, many thanks for doing that.

My base class is now working perfectly.