简体   繁体   中英

Click Event for button in ListView DataTemplate not firing

I've searched through many similar questions, but none seem to handle this situation. I'm trying to open a context menu through both a right click on the list view item (working) and when a user left clicks on the more ellipses button (not working). This works on another part of my program where I do essentially the same thing in a tree view, and both the ellipses button and right clicking on the treeviewitem opens the context menu. In this case however, clicking on the ellipses button does not open the context menu, only right clicking the tile works. It doesn't even hit the break point in the code behind of the button clicked handler function.

I have a ListView set up as follows:

    <ListView x:Name="listViewStudents" 
            ItemsSource="{Binding Students}"
            SelectionChanged="StudentList_SelectionChanged">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Height" Value="36"></Setter>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.ContextMenu>
                        <ContextMenu x:Name="StudentContextMenu">
                            <ContextMenu.Items>
                                <MenuItem Header="View Details"                               
                                      Command="{Binding Source={x:Reference studentDetailsView}, Path=DataContext.OpenLinkCommand}"
                                      CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext}">
                                </MenuItem>
                            </ContextMenu.Items>
                        </ContextMenu>
                    </Grid.ContextMenu>
                    <TextBlock Grid.Column="1" Margin="0" Text="{Binding Name}" Foreground="Black" FontFamily="{StaticResource FontFamilyBody1}" FontSize="15"/>
                    <Button Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" Click="MoreButton_Click" Width="50" Margin="0,0,-14,0">
                        <materialDesign:PackIcon Kind="MoreVert"/>
                    </Button>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

And here is the code behind:

    private void MoreButton_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        ContextMenu menu = (ContextMenu)(button).FindName("StudentContextMenu");
        menu.DataContext = button.DataContext;
        menu.IsOpen = true;
    }

So why wouldn't this ListView with a button in the datatemplate call the function to open a context menu? And why would it work on a TreeView, but not here? Any help is greatly appreciated.

EDIT : I commented out the EventSetter "PreviewMouseLeftButtonDown" as well as the handling function in the code behind, and now the button in the xaml seems to work just fine (it is opening the context menu). But I need that EventSetter and handler for some of the logic of the app. Why would the PreviewMouseLeftButtonDown event prevent the button event? Is there a way to get both to work?

<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown"/>

Try replacing the event PreviewMouseLeftButtonDown with MouseLeftButtonUp or PreviewLeftButtonUp . The PreviewMouseLeftButtonDown could be eating up the event that would otherwise get handled by your button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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