0

I made a main window that displays various user controls in a content control. In this window, I have the user controls and their accompanying view models in the XAML as DataTemplate Resources. This window has a button that needs to display the user control in the contentcontrol and instantiate the view model for it. How can i pass the resource to my RelayCommand, so that i can tell the command which user control and view model to use? I figured out how to pass a hard-coded string as the command parameter, but now I'm wanting to pass the x:Name so i can reuse this command etc for more than one View-ViewModel.

Main Window's XAML snippets:

<Window.Resources>
    <!--User Controls and Accompanying View Models-->
    <DataTemplate DataType="{x:Type EmployerSetupVM:EmployerSetupVM}" x:Key="EmployerSetup" x:Name="EmployerSetup">
        <EmployerSetupView:EmployerSetupView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type VendorSetupVM:VendorSetupVM}">
        <VendorSetupView:VendorSetupView />
    </DataTemplate>
</Window.Resources>

<Button Style="{StaticResource LinkButton}" Command="{Binding ShowCommand}" CommandParameter="{StaticResource EmployerSetup}">

... In the Main Window's ViewModel, here relevant code so far:

public RelayCommand<DataTemplate> ShowCommand
    {
        get;
        private set;
    }

ShowCommand = new RelayCommand<string>((s) => ShowExecuted(s));



private void ShowExecuted(DataTemplate s)
    {
        var fred = (s.DataType);  //how do i get the actual name here, i see it when i hover with intellisense, but i can't access it!


        if (!PageViewModels.Contains(EmployerSetupVM))
        {
            EmployerSetupVM = new EmployerSetupVM();
            PageViewModels.Add(EmployerSetupVM);
        }

        int i = PageViewModels.IndexOf(EmployerSetupVM);

        ChangeViewModel(PageViewModels[i]);
    }

... in other words, how do i get the name of the my DataTemplate w/ x:Key="EmployerSetup" in the XAML? If it matters, I'm using MVVMLight too

  • so how does this `Button` know if it should pass `VendorSetupVM` or `EmployerSetupVM`. I don't follow your question and your xaml and VM code aint in sync. In xaml your binding to `EmployerSetupCommand` while in .cs that's called `ShowCommand` I guess? What's `PageViewModels` and why are you in that function only checking for `EmployerSetupVM`. Just type some pseudo-code of what your expecting in a clear way. No clue what's even going on in this. Don't really see the benefit of reusing a command with all this complexity tbh. I'd just have 2 commands and switch the Binding in the Button Style. – Viv Jun 08 '13 at 00:23
  • sorry, I edited to make the command names correct etc. My question is how to correctly use CommandParameter to tell the function which ViewModel I should have the code change the current view model to. PageViewModels is simply a collection of ViewModels. – Theodosius Von Richthofen Jun 10 '13 at 12:55
  • basically I just need to know, if I pass the DataTemplate "EmployerSetup" which respresents my EmployerSetup ViewModel, how do I get the name for it in code? My code right now can only get the FullName if I just take fred.ToString() – Theodosius Von Richthofen Jun 10 '13 at 13:15

1 Answers1

1

Try using the Name property of the class Type:

private void ShowExecuted(DataTemplate s) {
  var typeName = s.DataType as Type;
  if (typeName == null)
    return;
  var className = typeName.Name;  // className will be EmployerSetupVM or VendorSetupVM
  ...
}

I'd still say passing the DataTemplate to the VM just seems strange. I'd just have two commands and switch the command used in the Button.Style according to the conditions you got.

If you "have" to use a single RelayCommand or the world might end, I'd tend to use a static enum that you can reference from xaml for CommandParameter than pass the whole DataTemplate object.

Viv
  • 17,170
  • 4
  • 51
  • 71