C# Caliburn.Micro列表框,上下文菜单未找到目标
我有一个MasterView,它有一个与ListView绑定的TabControl。我将列表拆分为一个辅助视图,因为我想将这两个视图分开。ListView必须执行一些与MasterView没有共同之处的操作 这里是MasterView.xaml的代码C# Caliburn.Micro列表框,上下文菜单未找到目标,c#,xaml,mvvm,contextmenu,caliburn.micro,C#,Xaml,Mvvm,Contextmenu,Caliburn.micro,我有一个MasterView,它有一个与ListView绑定的TabControl。我将列表拆分为一个辅助视图,因为我想将这两个视图分开。ListView必须执行一些与MasterView没有共同之处的操作 这里是MasterView.xaml的代码 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w
<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\u Open的函数。我想在ListView中添加一个菜单,该菜单调用MasterViewModel.cs的菜单项打开
下面是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\u New的错误“未找到目标”
我认为这个问题与视觉树被破坏有关,我尝试了更多的解决方案,但每次失败,我都会得到同样的错误。
有解决这个问题的提示吗
编辑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>
问题可能在于,MenuItems的数据上下文不是MasterViewModel,而是ListBox的DataContext 如果我从嵌套项访问viewModel的属性/方法,我只使用以下方法:
LayoutRoot是位于根目录中的元素的名称,因此其数据上下文是ViewModel的数据上下文,您也可以使用相同名称的网格。然后在您所说的路径中,您希望访问该元素的数据上下文,然后您可以访问该元素的方法/属性。这应该也适用于您。我使用编辑1更新问题:我尝试添加您的按钮以检查是否有效,但我得到:System.Windows.Data错误:4:找不到引用为“ElementName=LayoutRoot”的绑定源。BindingExpression:Path=DataContext.MenuItem\u New;DataItem=null;目标元素是“按钮”名称=;目标属性为“命令”类型“ICommand”