1

I'm trying to expose the ViewModel as a static resource on the page so that it can be easily accessible by the binding.

TestViewModel.cs

namespace Test.WPFUI.Home
{ 
    public class TestViewModel....

HelloWorldView.Xaml

xmlns:local="clr-namespace:Test.WPFUI.Home"

<UserControl.Resources>
    <local:TestViewModel x:Key="mainPageViewModel" />
</UserControl.Resources>

TestViewModel Can't be found. May I ask for some tips or suggestions Please.

Getting help from http://www.telerik.com/help/silverlight/gridview-troubleshooting-blank-cells.html

public class LoanViewModel : ScreenViewModelBase<LoanViewModel>, IRecord, INotifyPropertyChanged 
{

    public LoanViewModel(IEventAggregator events) .............
Master
  • 2,038
  • 2
  • 27
  • 77
  • Hi @BradleyDotNET I did and still the same drop downs, and my Viewmodel doesn't appear. – Master Nov 24 '14 at 22:19
  • 1
    Its not in a different assembly is it? Did you handcraft that xmlns or use the auto-helper? – BradleyDotNET Nov 24 '14 at 22:20
  • Hi @BradleyDotNET I set the correct Assembly. "Local:TestViewModel" is underlined and it says No default constructor Found. May I ask what may be the causes and how to fix it? – Master Nov 25 '14 at 17:14
  • Ah; no default constructor, did you *define* a default constructor? Remember, defining your own parameterized one removes the default. – BradleyDotNET Nov 25 '14 at 17:15
  • No I haven't, not quite sure how to do that within the ViewModel. – Master Nov 25 '14 at 17:18
  • I've added the ViewModel I'm working with if it helps – Master Nov 25 '14 at 17:25

1 Answers1

1

It sounds like your initial problem was not having the full xmlns definition. You usually need both the namespace and assembly.

The easiest way to get it right, in my experience, is to let intellisense do it for you. Just start typing the namespace you want, and as long as its in a referenced project, there will be an autocomplete option.

Your second problem is due to not having a default constructor. You wrote this:

<local:TestViewModel x:Key="mainPageViewModel" />

Which will invoke the default constructor. However, you define a constructor here:

public LoanViewModel(IEventAggregator events) .............

Which removes the provided (paramaterless) default constructor. I'm going to take a wild guess and say that creating the correct IEventAggregator is not simple or desired from XAML, so I see two choices:

  1. You didn't really need that parameter in the constructor. Simply add a default constructor to your view model and you are good to go!

  2. You really need that parameter, so instantiating from XAML just isn't a good idea. Pass in your view model from somewhere else on the view's constructor.

If you feel like you can instantiate the correct object from XAML, use this post to invoke the paramaterized constructor: Calling a parameterized constructor from XAML

In my opinion, putting truly regular classes into XAML is not a good pattern to follow, so I wouldn't. By regular, I mean not related at all to the view.

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117