Redaa
Redaa

Reputation: 1590

WPF Button Click Event not working

I have a button that contains an Image inside a grid, my problem is that I can't make the button Click event work.

My XAML code:

....
<TabControl TabStripPlacement="Left">
    <TabItem Loaded="ListProductsLoaded" Width="185" Height="100" Header="Lister les Produits">
        <Grid Background="#FFE5E5E5">
            <ListView Name="ProductsListView" IsHitTestVisible="False">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="120" Header="Reference" DisplayMemberBinding="{Binding ProductReference}"></GridViewColumn>
                        <GridViewColumn Width="150" Header="Nom" DisplayMemberBinding="{Binding ProductName}"></GridViewColumn>
                        <GridViewColumn Width="120" Header="Photo" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button PreviewMouseLeftButtonDown="ImageButtonLeftMouseButtonDown" Name="ImageButton" Width="120" Height="120" Click="ImageButtonClicked">
                                        <Image Width="119" Height="119" Name="ProdImage" Source="{Binding ProductImage}"></Image>
                                    </Button>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </TabItem>
</TabControl>
....

As you can see I used two events, PreviewMouseLeftButtonDown event and Click event, I used PreviewMouseLeftButtonDown due to a solution I saw in a stackoverflow question but it didn't work. Both events methods display a MessageBox

private void ImageButtonClicked(object sender, RoutedEventArgs e)
{
    MessageBox.Show(this, "clicked image button");
}

private void ImageButtonLeftMouseButtonDown(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("test clicked");
}

If anyone encountered this problem before please help me solve it, I searched for a solution without success. Thanks in advance.

Edit: Even if I use a normal button: No image in it, it doesn't work.

Solution : Actually, i set the Option IsHitTestVisible of the TabControl to false and that disabled all Click events… sorry everybody.

Upvotes: 5

Views: 21547

Answers (4)

Athansios Savvidis
Athansios Savvidis

Reputation: 31

The Click event of the button is working. Remove any breakepoints in the code that might be triggered between your click and the button's OnClick() method. (e.g. OnPreviewMouseDown, etc) I don't know how internally the Click or MouseDoubleClick events are triggered / differentiated but logically should be the clock involved analyzing the time between the mouse-down and mouse-up intervals. If after your mouse-down on the button there is a breakpoint in the code, the timer analysis is not recognizing a Click event.

Upvotes: 0

Alexander Bell
Alexander Bell

Reputation: 7918

You can remove all event subscription from XAML and put it in code behind using compact syntax (just a single line of code) as shown below:

ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };

Also, REMOVE your event handling proc:

private void ImageButtonClicked(object sender, RoutedEventArgs e)
{
    MessageBox.Show(this, "clicked image button");
}

In addition to this, another event handler for the same button (PreviewMouseLeftButtonDown="ImageButtonLeftMouseButtonDown" in XAML) seems redundant – you can remove it with corresponding event handling proc.

So, the code snippet should look like:

MainWindow()
{
    InitializeComponent();
    ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };

// ... the rest of the code...

Pertinent to your case (Button control in ListView template), refer to this article: WPF ListView with buttons on each line

Upvotes: 2

Sanjaya
Sanjaya

Reputation: 156

try using Tap event without using click event.Same function in both events,

Upvotes: 0

user3904868
user3904868

Reputation:

Your Click event is named Test_OnClick in codebehind but in XAML you refer to it as Click="ImageButtonClicked" so the event does not fire. It needs to be Click="Test_OnClick"

Upvotes: 0

Related Questions