我正在尝试将命令从功能区绑定到内容控件。
我的观点是这样的:
<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
andCountryViewModel
派生自EditorViewModel<T>
And 共享已放入EditorViewModel<T>
基类的基本动作
public RelayCommand NewCommand { get; private set; }
public RelayCommand SaveCommand { get; private set; }
ETC....
我的问题是如何将命令从子视图模型公开到功能区?
由于第一个视图模型 CurrentSubView 没有实现这些命令,所以我得到“ BindingExpression path error: 'NewCommand' property not found on 'object' ''MainViewModel'” .....
我设法通过我添加的 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; }
但仍然开放以获得更优雅的建议;)