我正试图将命令从功能区绑定到内容控件。
我的观点看起来像这样:
<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从IOC设置CurrentSubView:
CurentSubView = ViewModelFactory.Create<SomeViewModel>();
CityViewModel
和CountryViewModel
来自EditorViewModel<T>
并分享已放入EditorViewModel<T>
基类
public RelayCommand NewCommand { get; private set; }
public RelayCommand SaveCommand { get; private set; }
等...
我的问题是如何将命令从子视图模型公开到功能区?
作为第一个视图模型,CurrentSubView没有实现这些命令,因此我得到“ BindingExpression路径错误:在'对象'''MainViewModel'上找不到'NewCommand'属性” .....
我设法通过我添加的MainViewModel中的一些代码绑定命令:
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; }
但仍然打开更优雅的建议;)
答案 0 :(得分:0)
因此它很简单。
<fluent:RibbonTabItem/>
已将其DataContext
放在<fluent:RibbonTabItem Name="SomeTab" DataContext="{Binding CurrentViewModel}" Header="SomeTab">
所以只需要完成:
{{1}}