4

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.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Redaa
  • 1,540
  • 2
  • 17
  • 28
  • I had a similar problem: the `Button_Click`-Event didn't fire because I also had a `Button_PreviewMouseUp`-Event which contained `e.Handled=true`. After I removed it, the Click-Event worked. – MHN Nov 08 '20 at 15:10

4 Answers4

2

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

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • when i wanted to add the event in code behind, i couldn't access the button. in the window constructor, or even in the event method :o – Redaa Aug 03 '14 at 23:43
  • no one of the solutions u gave me worked, because i can't access the ImageButton property. – Redaa Aug 03 '14 at 23:50
  • Read the amended post, it explains how to do it. Rgds, – Alexander Bell Aug 03 '14 at 23:55
  • OK, I see the problem: this is not just a button, it's a Button in ListView Template. Take a look at this solution, it may help: http://stackoverflow.com/questions/456063/adding-button-into-a-listview-in-winforms – Alexander Bell Aug 03 '14 at 23:58
  • i know it's click event, i'm speaking about the Button control. XAML: CS: testButton.Click... Cannot Resolve symbol testButton – Redaa Aug 04 '14 at 00:01
  • Also, this one may help, either: http://stackoverflow.com/questions/7127920/wpf-listview-with-buttons-on-each-line/ Regards, – Alexander Bell Aug 04 '14 at 00:05
0

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"

  • No, this is not my exact xaml i had to change it. but the method ImageButtonClicked exists like Test_OnClick. – Redaa Aug 03 '14 at 23:35
  • Alex has a good solution, or see this http://stackoverflow.com/questions/4151380/wpf-image-control-with-click-event –  Aug 03 '14 at 23:43
  • In code behind, put the statement in my solution right after the line public MainWindow(){ InitializeComponent(); – Alexander Bell Aug 03 '14 at 23:50
0

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

Sanjaya
  • 156
  • 1
  • 9
0

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.