Question

I'm trying to figure hot to bind commands from ribbon to content control.

My view look something like this:

  <Window.Resources>
    <DataTemplate DataType="{x:Type VM:CityViewModel}">
        <Views:EditorView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type VM:CountryViewModel}">
      <Views:EditorView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type VM:SomeOtherViewModel}">
      <Views:SomeOtherView />
    </DataTemplate>
  </Window.Resources>
 <DockPanel>
    <Fluent:Ribbon x:Name="MainRibbon"
                   AutomaticStateManagement="True"
                   DockPanel.Dock="Top">
      <Fluent:RibbonTabItem Header=SomeHeader>
      <Fluent:RibbonGroupBox Header="Actions">
       <Fluent:Button Fluent:RibbonAttachedProperties.RibbonSizeDefinition="Middle,Small"
                      Header="New"
                      Command = "{Binding NewCommand}"
                      CommandTarget="{Binding ElementName=SubView}"/>
       <Fluent:Button Fluent:RibbonAttachedProperties.RibbonSizeDefinition="Middle,Small" Header="Save"
                      Command = "{Binding SaveCommand}"
                      CommandTarget="{Binding ElementName=SubView}"/>
      </Fluent:RibbonGroupBox>
     </Fluent:RibbonTabItem>
   </Fluent:Ribbon>           
  <ContentControl Name="SubView" Content="{Binding CurentSubView}" />
 </DockPanel>

MainViewModel sets CurrentSubView from IOC:

CurentSubView = ViewModelFactory.Create<SomeViewModel>();

CityViewModel and CountryViewModel are derived from EditorViewModel<T> And share basic Actions which have been put to EditorViewModel<T> base class

 public RelayCommand NewCommand { get; private set; }
 public RelayCommand SaveCommand { get; private set; }

etc....

The question for me is how to expose commands from child viewmodel to ribbon?

As first view model CurrentSubView does not implement those commands so I get "BindingExpression path error: 'NewCommand' property not found on 'object' ''MainViewModel'".....

I managed to bind command by some code in my MainViewModel i have added:

 private RelayCommand m_newCommand;
 public RelayCommand NewCommand
   {
     get { return m_newCommand; }
   }
  if (typeof(IEditorViewModel).IsInstanceOfType(CurentSubView))
  {
    m_newCommand = ((IEditorViewModel)CurentSubView).NewCommand;
    RaisePropertyChanged(() => NewCommand);
  }
  else
  {
    m_newCommand = null;
  }

But still opened for more elegant suggestions ;)

Was it helpful?

Solution

So it was simple as it can be. <fluent:RibbonTabItem/> has it's on DataContext So only needed to be done:

<fluent:RibbonTabItem Name="SomeTab" DataContext="{Binding CurrentViewModel}" Header="SomeTab">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top