使用ContextMenu的Caliburn.Micro ListBox找不到目标

时间:2015-03-05 10:02:21

标签: c# xaml mvvm contextmenu caliburn.micro

我有一个MasterView,它具有与ListView的TabControl绑定。我将List拆分为二级视图,因为我想将两个视图分开。 ListView必须执行一些与MasterView没有任何共同点的操作。

这里是MasterView.xaml的代码

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:view="clr-namespace:App"
    mc:Ignorable="d" x:Class="App.MasterView"
    Title="Setup Cylinder"  
    WindowStartupLocation="CenterScreen" 
    Height="732" Width="986"  >
<Grid Margin="0,0,0,0" Background="#FF837F80" >
    <TabControl SelectedIndex="{Binding CycleIndex}" Grid.Column="0" ItemsSource="{Binding Items}" Margin="0,5,0,10">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl cal:View.Model="{Binding}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTabStop="False" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

在MasterViewModel.cs中有一个名为void public MenuItem_Open()的函数。我想在ListView中添加一个菜单,调用MasterViewModel.cs的MenuItem_Open()。

以下是ListView.xaml的代码

<UserControl x:Class="App.ListView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cal="http://www.caliburnproject.org"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:App"
         mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="517.5">
<Grid Margin="0,0,0,0" x:Name="LayoutRoot">
    <ScrollViewer   Margin="0,0,0,10" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <ListBox  SelectedIndex="{Binding SelectedStepIndex}" x:Name="Steps" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  HorizontalContentAlignment="Left" VerticalContentAlignment="Center" AlternationCount="2">
            <ListBox.ContextMenu>
                <ContextMenu >
                    <MenuItem Header="New"  cal:Message.Attach="[Event Click] = [Action MenuItem_New()]"/>
                    <MenuItem Header="Copy"  />
                    <MenuItem Header="Paste" />
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>
    </ScrollViewer>
  </Grid>

问题始终是错误“找不到MenuItem_New()的目标”。

我认为这个问题与Visual Tree Broken有关,我尝试了更多的解决方案,但每次我都失败了,我得到了同样的错误。 有什么提示可以解决这个问题吗?

编辑1:绑定错误

<UserControl x:Class="App.ListView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cal="http://www.caliburnproject.org"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:App"
         mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="517.5">
<Grid Margin="0,0,0,0" x:Name="LayoutRoot">
    <ScrollViewer   Margin="0,0,0,10" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <ListBox  SelectedIndex="{Binding SelectedStepIndex}" x:Name="Steps" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  HorizontalContentAlignment="Left" VerticalContentAlignment="Center" AlternationCount="2">
            <ListBox.ContextMenu>
                <ContextMenu >
                    <MenuItem Header="New"  cal:Message.Attach="[Event Click] = [Action MenuItem_New()]"/>
                    <MenuItem Header="Copy"  />
                    <MenuItem Header="Paste" />
<Button Command="{Binding ElementName=LayoutRoot, Path=DataContext.MenuItem_New}"/>
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>
    </ScrollViewer>
  </Grid>

1 个答案:

答案 0 :(得分:0)

问题是(可能),MenuItems的数据上下文不是MasterViewModel,而是ListBox的DataContext。

如果我从嵌套项访问viewModel的属性/方法,我只想使用它:<Button Command="{Binding ElementName=LayoutRoot, Path=DataContext.ShowSessionDetail}" CommandParameter="{Binding SessionId}">

LayoutRoot是元素的名称,它位于root中,因此它的数据上下文是ViewModel的数据上下文(您还有同名的Grid)。然后在Path中,您想要访问该元素的数据上下文,然后您可以访问它的方法/属性。这对你也有用。