【问题标题】:Caliburn.Micro ListBox with ContextMenu No Target Found带有 ContextMenu 的 Caliburn.Micro ListBox 未找到目标
【发布时间】:2015-03-05 10:02:21
【问题描述】:

我有一个与 ListView 绑定 TabControl 的 MasterView。我将列表拆分为辅助视图,因为我想将这两个视图分开。 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>

问题总是出现错误“No Target found for 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>

【问题讨论】:

    标签: c# xaml mvvm contextmenu caliburn.micro


    【解决方案1】:

    问题是(可能),MenuItems 的 Data Context 不是 MasterViewModel,而是 ListBox 的 DataContext。

    如果我从嵌套项访问 viewModel 的属性/方法,我只使用这个:&lt;Button Command="{Binding ElementName=LayoutRoot, Path=DataContext.ShowSessionDetail}" CommandParameter="{Binding SessionId}"&gt;

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

    【讨论】:

    • 我使用编辑 1 更新问题:我尝试添加您的按钮以检查是否有效,但我得到:System.Windows.Data 错误:4:找不到与参考 'ElementName=LayoutRoot' 绑定的源. BindingExpression:路径=DataContext.MenuItem_New;数据项=空;目标元素是 'Button' (Name='');目标属性是“Command”(输入“ICommand”)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多