Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何传递Datatemplate';s数据类型到Caliburn.Micros消息。是否附加?_C#_Wpf_Xaml_Data Binding_Caliburn.micro - Fatal编程技术网

C# 如何传递Datatemplate';s数据类型到Caliburn.Micros消息。是否附加?

C# 如何传递Datatemplate';s数据类型到Caliburn.Micros消息。是否附加?,c#,wpf,xaml,data-binding,caliburn.micro,C#,Wpf,Xaml,Data Binding,Caliburn.micro,我目前正在为一个小组项目实施一个菜单导航系统。我们正在使用Caliburn.Micro和MVVM模式 该菜单位于ShellView.xaml中,并基于使用角色属性筛选的列表 public class NavigationMenuItem { public IScreen ViewModel { get; set; } public string IconPath { get; set; } public string Role { get

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

该菜单位于
ShellView.xaml
中,并基于使用角色属性筛选的
列表

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>
如何将给定
导航菜单的ViewModel属性传递给
句柄(屏幕)
方法

编辑 添加了每个请求的句柄方法

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

也许你可以试试这样的东西,如前所述:


这将把完整视图模型(
DataContext
)传递给处理程序

要传递视图模型的特定属性,请使用所示的长语法:


i
定义为

xmlns:i=“clr命名空间:System.Windows.Interactivity;assembly=System.Windows.Interactivity”
您还可以将
按钮的
DataContext
属性更改为感兴趣的属性,如:


因此,只有正确的属性才能传递给您的hendler,但这感觉像是一次黑客攻击。
单击后,这会将
按钮的
DataContext
作为
Handle
方法的参数进行移交,该方法应为
NavigationMenuItem

也许您可以尝试类似的方法,如下所述:


这将把完整视图模型(
DataContext
)传递给处理程序

要传递视图模型的特定属性,请使用所示的长语法:


i
定义为

xmlns:i=“clr命名空间:System.Windows.Interactivity;assembly=System.Windows.Interactivity”
您还可以将
按钮的
DataContext
属性更改为感兴趣的属性,如:


因此,只有正确的属性才能传递给您的hendler,但这感觉像是一次黑客攻击。
单击后,这会将
按钮的
DataContext
作为
Handle
方法的参数移交,该方法应该是
NavigationMenuItem

您的意思是“如何传递”而不是解析吗?我当然可以。谢谢你的更正!嗯,句柄是EventAgegator的,你能显示你的shellview/shellviewmodel代码吗?我在OP中添加了完整的方法。我只是在使用聚合器传递屏幕。你是说“我如何传递”而不是解析?我当然可以。谢谢你的更正!嗯,句柄是EventAgegator的,你能显示你的shellview/shellviewmodel代码吗?我在OP中添加了完整的方法。我只是使用聚合器来传递屏幕。这是可行的,但我想传递项目的特定属性。好吧,我有进展了,但actionMessage发送的对象似乎不是“屏幕”类型. 我可以通过创建一个句柄(对象值)来接收它,然后对其进行重铸。关于如何获得正确的数据类型,您有什么想法吗?能否显示
NavigationMenuItem
类的代码?可能您的
ViewModel
属性不是
Screen
类型?您收到的实际类型是什么?如果没有更多的信息,就很难提供帮助。这是可行的,但我想传递项目的特定属性。好吧,我有进展了,但actionMessage发送的对象似乎不是“Screen”类型。我可以通过创建一个句柄(对象值)来接收它,然后对其进行重铸。关于如何获得正确的数据类型,您有什么想法吗?能否显示
NavigationMenuItem
类的代码?可能您的
ViewModel
属性不是
Screen
类型?您收到的实际类型是什么?没有更多的信息,就很难提供帮助。
public void Handle(Screen screen)
        {
            if (screen is LoginViewModel)
            {
                SidebarIsVisible = false;
                NotifyOfPropertyChange(() => SidebarIsVisible);
            }
            else
            {
                SidebarIsVisible = true;
                NotifyOfPropertyChange(() => SidebarIsVisible);
            }
            ActivateItem(screen);
        }