如何将 Datatemplate 的数据类型 属性 传递给 Caliburn.Micros Message.Attach?

How do I pass a property of Datatemplate's Datatype to Caliburn.Micros Message.Attach?

我目前正在为一个小组项目实施菜单导航系统。我们正在使用 Caliburn.Micro 和 MVVM 模式。

菜单位于 ShellView.xaml 中,基于 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) 方法?

编辑 为每个请求添加了 Handle 方法

public void Handle(Screen screen)
        {
            if (screen is LoginViewModel)
            {
                SidebarIsVisible = false;
                NotifyOfPropertyChange(() => SidebarIsVisible);
            }
            else
            {
                SidebarIsVisible = true;
                NotifyOfPropertyChange(() => SidebarIsVisible);
            }
            ActivateItem(screen);
        }

也许你可以尝试这样的事情,正如解释的那样 here:

<Button cal:Message.Attach="[Event Click] = [Action Handle($dataContext)]" Style="{StaticResource BtnSidebar}">
    <Image Margin="20" Source="{Binding IconPath}"/>
</Button>

这会将完整的视图模型 (DataContext) 传递给您的处理程序。

要传递视图模型的特定属性,请使用here:

中所示的长语法
<Button Style="{StaticResource BtnSidebar}">
    <i:Interaction.Triggers> 
        <i:EventTrigger EventName="Click"> 
            <cal:ActionMessage MethodName="Handle"> 
               <cal:Parameter Value="{Binding ViewModel}" /> 
            </cal:ActionMessage> 
        </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <Image Margin="20" Source="{Binding IconPath}"/>
</Button>

i 定义为

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

您还可以将 ButtonDataContext 属性 更改为感兴趣的 属性,例如:

<Button DataContext="{Binding ViewModel}" cal:Message.Attach="[Event Click] = [Action Handle($dataContext)]" Style="{StaticResource BtnSidebar}">
    <Image Margin="20" Source="{Binding IconPath}"/>
</Button>

因此只有正确的 属性 会传递给您的处理程序,但这感觉就像是 hack。 单击时,这将移交 ButtonDataContext 作为您的 Handle 方法的参数,它应该是 NavigationMenuItem.