Search code examples
c++-winrt

Button Click event fires twice, even with ClickMode set to "Press"


A XAML Button's Click even fires twice with a left-button mouse click.

I added the XAML Button Property ClickMode = "Press", but that made no difference. The Microsoft documentation for ClickMode indicates that that should have solved the problem.

I discovered this with a breakpoint in the C++ code for the Click event handler, which stopped the code twice for each button click.

XAML code within a Grid on a Page container:

        <Button x:Name="button_listFiles" Grid.Column="1" Grid.Row="0" Content="List" HorizontalAlignment="Center" 
                        Click="button_listFiles_Click" ClickMode="Press"></Button>

C++ Click event handler:

    void MainPage::button_listFiles_Click(IInspectable const& sender, RoutedEventArgs const& e) {

        MainPage::counter++;

        textBox_directoryName().Text(to_hstring(counter));
    }

The MainPage::counter++; counts the number of times the handler is called and textBox_directoryName().Text(to_hstring(counter)); displays the result on the XAML page, used for attempted debugging.


Solution

  • Just to close this out, the answer is the several comments between me and Kenny Kerr.