Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Toolkit Extended - Bind Dropdown Button Closed event to ViewModel method

Tags:

c#

mvvm

wpf

I'm trying to convert a C# WPF application to using MVVM. The DropDownButton I'm having an issue with is from Xceed's WPF Extended Toolkit. I'm trying to bind the closed event of the dropdownbutton to a method in my ViewModel. When the button's context menu is closed the method doesn't fire at all.

When I change the eventname to Click, the method fires no problem. I want the method to fire once the user has selected one of the items in the dropdown context menu rather than when the button itself is selected. I have tried to set the DataContext of the Context Menu (as per WPF ContextMenu woes: How do I set the DataContext of the ContextMenu?) and use a click event for the menu items themselves but with the same result - no errors but the method doesn't fire. I've also tried using different event names such as DropDownClosing, DropDownClosed, Closing and IsClosed with no success.

        <xcad:DropDownButton Name="weekMonthDropButton" Background="White" Content="Chart By Week/Month" Width="150"  Grid.Row="0" Grid.ColumnSpan="2">
            <xcad:DropDownButton.DropDownContextMenu>
                <ContextMenu>
                    <MenuItem Header="Week">

                    </MenuItem>
                    <MenuItem Header="Month"/>

                </ContextMenu>

            </xcad:DropDownButton.DropDownContextMenu>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Closed">
                    <ei:CallMethodAction TargetObject="{Binding}" MethodName="WeekMonthMenuClicked" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </xcad:DropDownButton>

I'm guessing that either I've been making a mistake in the event name or in the setting of the context but I'm new to both WPF and MVVM and I'm having difficulty debugging the problem. Any help would be greatly appreciated

like image 451
S1984 Avatar asked Jun 27 '26 20:06

S1984


1 Answers

If it might help anyone else - I ended up going down the ICommand route and setting the DataContext for the individual MenuItem. The following code worked for me.

<xcad:DropDownButton Name="weekMonthDropButton" Background="White" Content="Chart By Week/Month" Width="150"  Grid.Row="0" Grid.ColumnSpan="2">
            <xcad:DropDownButton.DropDownContextMenu>
                <ContextMenu >
                    <MenuItem Header="Week" Command="{Binding Path=PlacementTarget.DataContext.WeekMonthMenuCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                    <MenuItem Header="Month"/>
                </ContextMenu>

            </xcad:DropDownButton.DropDownContextMenu>

        </xcad:DropDownButton>
like image 162
S1984 Avatar answered Jun 30 '26 11:06

S1984



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!