MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
15.04k stars 3.41k forks source link

Splitbutton Click trigger when I click on a popup content element #3643

Closed JeanPhilippeLux closed 1 month ago

JeanPhilippeLux commented 1 month ago

Bug explanation

Hi,

I use the splitbutton. If I add a button in the popupcontent, when I click on it, the click event of the button of the popupcontent is fired, but also the Click event of the splitbutton.

here is my code :

                    <materialDesign:SplitButton  Style="{StaticResource MaterialDesignOutlinedSplitButton}" Height="32" materialDesign:ButtonAssist.CornerRadius="7" PopupPlacementMode="BottomAndAlignRightEdges" PopupUniformCornerRadius="7" Click="BtExportCard_Click" >

                        <materialDesign:SplitButton.Content>
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="FileExportOutline" Foreground="{DynamicResource MaterialDesign.Brush.Primary}"></materialDesign:PackIcon>
                                <TextBlock Text="Changement de carte" Foreground="{DynamicResource MaterialDesign.Brush.Primary}"></TextBlock>
                            </StackPanel>

                        </materialDesign:SplitButton.Content>

                        <materialDesign:SplitButton.PopupContent>
                            <Button Content="Export Excel" PackIconKing="{materialDesign:PackIcon Kind=FileExcelBoxOutline}" Click="ExportCardExcel"></Button>
                        </materialDesign:SplitButton.PopupContent>

                    </materialDesign:SplitButton>

If I click on the button of the popupcontent, the "ExportCardExcel" will be fired and afhter the "BtExportCard_Click".

can someone help me?

thank you. Jean-Philippe.

Version

5.1

corvinsz commented 1 month ago

@JeanPhilippeLux After some testing I noticed the click event of the inner button is bubbling up to the left button of the SplitButton (called PART_LeftButton).

Since I'm somewhat new to this library I'm not sure how to fix it in the control itself, but a quick-fix on your side would be to mark the event as handled in your code behind like this:

private void ExportCardExcel(object sender, RoutedEventArgs e)
{
    //do excel stuff here
    e.Handled = true;
}

P.S.: Your code does not compile, PackIconKing is not a property on a Button. Consider putting a stackpanel containing an icon and text inside of the button if that is want you want.

JeanPhilippeLux commented 1 month ago

Hi, Thank you for your reply. Yes my button is not the standard button, it's a homemade User control of type StdOpeButton and he has a PackIconKing property.

For the moment I do thak to fix : I check the type of source and if it's not a StdOpeButton, that means I Click on the submenu element of type StdOpeButton and not on the main button.

private async void BtExportCard_Click(object sender, RoutedEventArgs e) { if (e.OriginalSource is not StdOpeButton) { await viewModel.ExportCard(false); } }

the click in the submenu still like that :

private async void ExportCardExcel(object sender, RoutedEventArgs e) { await viewModel.ExportCard(true); }

Keboo commented 1 month ago

Good analysis @corvinsz

I suspect a similar fix to the one you propose would be the right solution here. In the OnApplyTemplate of SplitButton register for the click event from the popup box, simply mark the event as handled. This should probably follow the same null checking and event registration that is being done for the _rightButton already.

corvinsz commented 1 month ago

created a pull request 3643 that should fix this issue