开发者

WPF How to catch ContextMenuClosing event

开发者 https://www.devze.com 2023-02-14 17:23 出处:网络
I have the following XAML: <Grid > <Grid.ColumnDefinitions> <ColumnDefinition Width=\"20\"/>

I have the following XAML:

<Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>            
        <local:DropDownButton 
            HorizontalAlignment="Right"
            Grid.Column="2"
            Width="18"
            Style="{StaticResource OrangeButton}"   
            ContextMenuClosing="colorPallete_ContextMenuClosing"
            x:Name="btnSelectColor">            
            <Polygon Points="0,0,5,4,10,0" Fill="Black"/>                                              
            <local:DropDownButton.DropDown>
                <ContextMenu StaysOpen="True" Name="colorPallete" ContextMenuClosing="colorPallete_ContextMenuClosing">
                    <MenuItem StaysOpenOnClick="True" OverridesDefaultStyle="True" ContextMenuClosing="colorPallete_ContextMenuClosing">
                        <MenuItem.Header>
                            <local:ColorPickerPopup x:Name="colorPicker" ContextMenuClosing="colorPallete_ContextMenuClosing"/>
                        </MenuItem.Header>                                                
                    </MenuItem>                                            
                </ContextMenu>                                        
            </local:DropDownButton.DropDown>
        </local:DropDownButton>            

        <Rectangle Width="17.5" Stroke="Black" Margin="0" 
                   Fill="{DynamicResourc开发者_如何学JAVAe CheckerBrush}"/>            

        <Rectangle Width="17.5" Margin="0" Name="rtcColorPreview" />
        <TextBox Margin="2,0,0,0" Grid.Column="1" 
                 Width="100" BorderThickness="0"                     
                 Text="{Binding ElementName=colorPicker, Mode=TwoWay,  Path=SelectedColorName}"/>

    </Grid>

The event handler colorPallete_ContextMenuClosing is not being called when ContextMenu labelled colrPallete is closing. I cannot seem to figure out what is missing.

Please help! TIA.


Per the MSDN documentation...

ContextMenu itself is a FrameworkElement derived class, but the ContextMenuClosing event will not be raised by a context menu directly. Instead, the event is raised from the element that "owns" the context menu as a property and is only raised when a user attempts to close a context menu in the UI.

You would need to adjust your code so that the handler is defined solely on the DropDownButton as you have done. If there is a nested ContextMenu then the nested ContextMenu will obviously raise the event.

<local:DropDownButton ContextMenuClosing="colorPallete_ContextMenuClosing">
        ...                             
</local:DropDownButton>

Using a Button it would look like this...

<Button ContextMenuClosing="ContextMenu_ContextMenuClosing">
    <Button.ContextMenu>
        <ContextMenu>
             <MenuItem Header="Go"/>
        </ContextMenu>
    </Button.ContextMenu>
 </Button>

..where when the ContextMenu containing the MenuItem closes; the event will be raised and the handler will be called.

Not certain what DropDownButton control you are using so I can't comment on what the DropDown property is and how you are nesting your ContextMenu.

0

精彩评论

暂无评论...
验证码 换一张
取 消