简体   繁体   English

在ListBoxItem中将Click事件附加到按钮的上下文MenuItem

[英]Attaching Click Event To Button's Context MenuItem Within ListBoxItem

I'm trying to create a download bar like chrome. 我正在尝试创建一个像chrome这样的下载栏。

有效的XHTML

The issue I'm currently having is trying to bind the click event to the button's context menu within the listboxitem. 我当前遇到的问题是尝试将click事件绑定到listboxitem中按钮的上下文菜单。 When the context menuitem is clicked, it says the action is not found. 单击上下文菜单项时,表示未找到该动作。

<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>

I could bind it behind code(xaml.cs) side of the application but I also lose track of what item is the context suppose to point to. 我可以将其绑定在应用程序的代码(xaml.cs)后面,但是我也无法跟踪上下文应该指向的项目。 To do that, i replaced caliburn's click event with a regular Click event. 为此,我将caliburn的click事件替换为常规的Click事件。 The SelectedItem and SelectedItems is null or empty, respectively. SelectedItem和SelectedItems分别为null或为空。

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

Haven't tested but something along these lines should open the context menu on right or left click: 尚未测试,但应遵循以下步骤打开鼠标右键或鼠标左键的上下文菜单:

<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>

As for the code-behind, the following worked for me in my last tug with a similar issue: 至于后面的代码,在我的上一次拖船中,以下内容为我解决了类似的问题:

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
        ...
    }
}

Let me know how these work for you 让我知道这些如何为您服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM