我的MainMenuView
中有一个部分,它在ContentControl
中加载选定的视图模型。DataContext
of MainMenuView
是MainMenuViewModel
。
我可以将主菜单中的按钮绑定到SelectedViewModel
上的命令,还是必须引发事件?
<Fluent:Ribbon>
<!--Userdata-->
<Fluent:RibbonTabItem Header="Data" x:Name="TabVerm" Group="{Binding ElementName=VermittlerGroup}">
<Fluent:RibbonGroupBox Header="Data">
<Fluent:Button" Command="{Binding Path=DataContext.VermittlerSave, ElementName=VermittlerView}" Header="Save" LargeIcon="{iconPacks:FontAwesome Kind=SaveRegular,Width=30,Height=25}"></Fluent:Button>
<Fluent:Button Header="Cancel" LargeIcon="{iconPacks:FontAwesome Kind=UndoAltSolid,Width=30,Height=25}"></Fluent:Button>
</Fluent:RibbonGroupBox>
<Fluent:RibbonGroupBox Header="Activate">
<Fluent:Button Header="Aktivate User" LargeIcon="{iconPacks:FontAwesome Kind=StackExchangeBrands,Width=30,Height=25}"></Fluent:Button>
<Fluent:Button Header="New User" LargeIcon="{iconPacks:FontAwesome Kind=PlusCircleSolid,Width=30,Height=25}"></Fluent:Button>
</Fluent:RibbonGroupBox>
</Fluent:RibbonTabItem>
</Fluent:Ribbon>
<Grid Grid.Row="1">
<ContentControl Grid.Row="1" Content="{Binding SelectedViewModel}"/>
</Grid>
发布于 2020-11-06 07:16:28
您应该能够通过给您的ContentControl
命名来做到这一点。
<ContentControl Grid.Row="1" x:Name="MyContentControl" Content="{Binding SelectedViewModel}"/>
然后,您可以引用它及其在命令绑定中保存视图模型的Content
属性,例如:
<Fluent:Button" Command="{Binding Content.VermittlerSave, ElementName=MyContentControl}" Header="Save" LargeIcon="{iconPacks:FontAwesome Kind=SaveRegular,Width=30,Height=25}"/>
当然,这仅适用于任何选定的视图模型都包含绑定命令。
绑定的替代方法是与事件通信,例如使用大多数MVVM框架(如Caliburn.Micro或Prism )提供的Caliburn.Micro,或者您可以查看Prism的CompositeCommand
方法,它允许您创建其他命令可以附加到的命令。您可以为菜单创建复合命令,并动态地将实际命令与所选视图模型连接或分离。
https://stackoverflow.com/questions/64705056
复制相似问题