【问题标题】:Attaching Click Event To Button's Context MenuItem Within ListBoxItem将单击事件附加到 ListBoxItem 内的按钮的上下文菜单项
【发布时间】:2016-10-11 16:47:23
【问题描述】:

我正在尝试创建一个像 chrome 一样的下载栏。

我目前遇到的问题是尝试将单击事件绑定到列表框项中按钮的上下文菜单。单击上下文菜单项时,它表示未找到该操作。

<ControlTemplate TargetType="{x:Type ListBoxItem}">
    <Button BorderBrush="Transparent" BorderThickness="0" telerik:StyleManager.Theme="Windows8" Click="ButtonBase_OnClick">
        <StackPanel Name="Panel" SnapsToDevicePixels="True" 
                Orientation="Horizontal" Margin="1 0"
                Height="30">

            <ContentControl Margin="0 0 10 0" Height="20">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="ContentTemplate" Value="{StaticResource Icons.File}"></Setter>                            
                    </Style>
                </ContentControl.Style>
            </ContentControl>

            <TextBlock Foreground="Black" Text="{Binding FileName}"  
                    VerticalAlignment="Center" 
                    TextAlignment="Center"
                    Margin="1 0 0 0"/>

            <Button x:Name="ExpandButton" Background="Transparent" Click="ExpandButton_OnClick" BorderThickness="0" VerticalAlignment="Center" ContextMenuService.IsEnabled="false">
                <Button.ContextMenu>
                    <ContextMenu x:Name="popup">
                        <MenuItem  Header="Open" cal:Message.Attach="[Click] = [Open($this)]"></MenuItem>
                    </ContextMenu>
                </Button.ContextMenu>
                <ContentControl ContentTemplate="{StaticResource Icons.ArrowUp}" Width="10" Height="10" Margin="2" VerticalAlignment="Center"/>
            </Button>
            <Rectangle Width="2" Fill="Gray" Margin="0 0 0 0"/>
        </StackPanel>
    </Button>
</ControlTemplate>

我可以将它绑定在应用程序的 code(xaml.cs) 端,但我也忘记了上下文应该指向的项目。为此,我将 caliburn 的 click 事件替换为常规的 Click 事件。 SelectedItem 和 SelectedItems 分别为 null 或空。

private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
    var originalSource = e.OriginalSource;
    var selectedItem = FileListBox.SelectedItem;
    var SelectedItems = FileListBox.SelectedItems;
}

【问题讨论】:

  • 我有点困惑你想要做什么。第一张照片是ListBoxItem,最右边有一个按钮,对吗?当您单击ListBoxItem 最右侧的按钮时,您希望上下文菜单打开?
  • @kalamazoowho 这正是我想要完成的。所以看起来你并不感到困惑。
  • 我已经用代码隐藏做过类似的问题,但我没有将上下文菜单弹出从右键单击更改为左键单击。您说您忘记了上下文指向的项目。你知道你可以从哪些项目中挑选吗?就像上下文菜单将指向的总是Button 类型?

标签: c# wpf


【解决方案1】:

尚未测试,但类似的内容应该通过右键或左键打开上下文菜单:

<Button x:Name="ExpandButton" Background="Transparent" Click="ContextMenu_Click" BorderThickness="0" VerticalAlignment="Center" ContextMenuService.IsEnabled="false">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu x:Name="popup" MenuItem.Click="menuItem_Click">
                        <MenuItem  Header="Open" cal:Message.Attach="[Click] = [Open($this)]"></MenuItem>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
    <ContentControl ContentTemplate="{StaticResource Icons.ArrowUp}" Width="10" Height="10" Margin="2" VerticalAlignment="Center"/>
</Button>

至于代码隐藏,在我上次的拔河中遇到了类似的问题,以下内容对我有用:

DependencyObject mainDep = new DependencyObject();

private void ContextMenu_Click(object sender, RoutedEventArgs e)
{
    DependencyObject dep = (DependencyObject)e.OriginalSource;

    while ((dep != null) && !(dep is ListBoxItem))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    mainDep = dep;
}
private void menuItem_Click(object sender, RoutedEventArgs e)
{
    DependencyObject dep = mainDep;

    if (dep is ListBoxItem)
    {
        ...
           DO your stuff here
        ...
    }
}

让我知道这些对你有什么作用

【讨论】:

  • 嗨@Hank,谢谢你的回答。这不太正确,但它为我指明了正确的方向。我一直在查找 VisualTree,直到我将 ListBoxItem 作为父级。
  • 很高兴听到它有帮助!因此,不是像我的回答那样搜索Button,而是搜索ListBoxItem?只是想确保为其他可能找到此帖子寻找答案的人更新答案
猜你喜欢
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
相关资源
最近更新 更多