【发布时间】:2019-10-12 13:01:09
【问题描述】:
我目前正在为一个小组项目实施菜单导航系统。我们正在使用 Caliburn.Micro 和 MVVM 模式。
该菜单位于ShellView.xaml 中,并且基于使用 Role 属性过滤的List<NavigationMenuItem>。
public class NavigationMenuItem
{
public IScreen ViewModel { get; set; }
public string IconPath { get; set; }
public string Role { get; set; }
public NavigationMenuItem(IScreen viewModel, string iconPath, string role)
{
ViewModel = viewModel;
IconPath = iconPath;
Role = role;
}
}
菜单显示得很好,但现在我想将 ViewModel 属性传递给 ShellViewModel.cs 中的一个方法,这样我就可以激活视图更改。这是ShellView.xaml 到目前为止的样子:
<Window
x:Class="P3GG.Views.ShellView"
xmlns:cal="http://www.caliburnproject.org"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:P3GG.Views"
xmlns:models="clr-namespace:P3GG.Models"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:templates="clr-namespace:P3GG.Templates"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="Title"
Width="800"
Height="600"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Visibility="{Binding SidebarIsVisible, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}"
Grid.Column="0"
Background="#333333"
x:Name="NavigationMenu">
<ListBox.ItemTemplate>
<DataTemplate DataType="models:NavigationMenuItem">
<Button cal:Message.Attach="Handle( <!-- pass content of NavigationMenuItem.ViewModel --> )" Style="{StaticResource BtnSidebar}">
<Image Margin="20" Source="{Binding IconPath}"/>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl Grid.Column="1" x:Name="ActiveItem"/>
</Grid>
</Window>
ShellViewModel.cs 目前包含两个 Handle 方法:
public void Handle(Screen screen)
public void Handle(User user)
如何将给定 NavigationMenu 的 ViewModel 属性传递给 Handle(Screen) 方法?
编辑 为每个请求添加了处理方法
public void Handle(Screen screen)
{
if (screen is LoginViewModel)
{
SidebarIsVisible = false;
NotifyOfPropertyChange(() => SidebarIsVisible);
}
else
{
SidebarIsVisible = true;
NotifyOfPropertyChange(() => SidebarIsVisible);
}
ActivateItem(screen);
}
【问题讨论】:
-
你的意思是“我如何通过”而不是解析?
-
我当然知道。谢谢指正!
-
嗯,Handle 是给 EventAgregator 的,能不能展示一下你的 shellview/shellviewmodel 代码?
-
我在 OP 中添加了完整的方法。我只是使用聚合器来传递屏幕。
标签: c# wpf xaml data-binding caliburn.micro