简体   繁体   中英

Why is my Button event is not occuring?

I am having an issue with my button event not occuring

Basically I have cart items that are listed in the listbox. When the delete button is clicked then the item is deleted from the list box.

I tried debugging, but it seems to not even call the method for when the button is clicked.

In my ticketscreen.xaml file I specify my button in the template:

<DataTemplate x:Key="TicketTemplate">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="50">
...
        <Button Name="Remove" Width="35" Height="35"
                FontFamily="Resources/#charlemagnestd-regular.otf" FontSize="24"
                Click="removeCartItem" Grid.Column="5"
                MouseMove="Remove_MouseMove">X</Button>
...
    </StackPanel>
</DataTemplate>

My List box is the following:

<ListBox Name="TicketItems" ItemsSource="{Binding}"
         ItemTemplate="{StaticResource TicketTemplate}"
         Grid.Row="3" Grid.ColumnSpan="6" Background="Transparent"
         BorderBrush="Transparent" IsHitTestVisible="False">
</ListBox>

My method removeCartItem is in the ticketscreen.xaml.cs:

private void removeCartItem(object sender, RoutedEventArgs e)
{
    Console.WriteLine("TestingCartRemove");
}

Am I missing something obvious? Thx in adv! :)

Edit: There seems to be something infront of it... maybe the listbox? How do I make it so that I am not clicking the ListBox, but I can click on things within the Stackpanel, which are contents of the list box.

Are you sure is not firing? Maybe you haven't seen the output in Visual Studio Output Window. Try to call a MessageBox.Show("Test"); instead.

You have a listbox control, which leads me to believe that this is not a console application. Therefore, Console.WriteLine() will not show you anything. Try MessageBox.Show() instead.

IsHitTestVisible="False" for the ListBox is disabling the click event for the Button. It makes all content within the ListBox invisible to hit-test as well.

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