开发者

ContextMenu closing when clicking an inner control

开发者 https://www.devze.com 2023-02-14 01:11 出处:网络
I have a textbox with a ContextMenu, the menu contains a tab control. The problem I\'m running into is that when I click any of the tab control items to change the visible tab control item the Context

I have a textbox with a ContextMenu, the menu contains a tab control. The problem I'm running into is that when I click any of the tab control items to change the visible tab control item the Context Menu gets closed. Any ideas?

I've tried the ContextMenu_ContextMenuClosing event but it doesn't appear to catch the event

<TextBox x:Name="TestCB4" TextSearch.TextPath="Name" >
    <TextBox.ContextMenu>
        <ContextMenu ContextMenuOpening="ContextMenu_ContextMenuOpening" 
                     ContextMenuClosing="ContextMenu_ContextMenuClosing"
                     MouseLeftButtonDown="ContextMenu_MouseLeftButtonDown"
                     StaysOpen="True"
                     Background="Transparent" Margin="0">
            <TabControl ContextMenuClosing="TabControl_ContextMenuClosing" Margin="0">
                <TabItem Header="User Space">
                    <WrapPanel Orientation="Horizontal"
                               HorizontalAlignment="Right"
                               Margin="10,2,2,2">
                        <Button Content="_Select" Margin="1"
                                Command="{Binding Path=SelectVendorCommand}" 
                                CommandParameter="{Binding ElementName=ucFindVendorCtrl, Path=VendorListView.SelectedItems}" />
                        <Button Content="_Add To User Space" Margin="1"
                                Command="{Binding Path=AddVendorToUserSpaceCommand}"
                                Width="120"
                                CommandParameter="{Binding ElementName=ucFindVendorCtrl, Path=VendorListView.SelectedItems}" />
                    </WrapPanel>
                </TabItem>
                <TabItem Header="Find">
                    <WrapPanel Orientation="Horizontal"
                               HorizontalAlignment="Right"
                               Margin="10,2,2,2">
                        <Button Content="_Select" Margin="1"
                                Command="{Binding Path=SelectVendorCommand}" 
              开发者_JAVA技巧                  CommandParameter="{Binding ElementName=ucFindVendorCtrl, Path=VendorListView.SelectedItems}" />
                        <Button Content="_Add To User Space" Margin="1"
                                Command="{Binding Path=AddVendorToUserSpaceCommand}"
                                Width="120"
                                CommandParameter="{Binding ElementName=ucFindVendorCtrl, Path=VendorListView.SelectedItems}" />
                    </WrapPanel>
                </TabItem>
            </TabControl>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>


I think that the problem is that the TabControl does not handle the propagation of the MouseLeftButtonDown routed events, and so the ContextMenu catches it, and closes.

If that's the case, then the solution is rather simple. Just wrap the TabControl in a ContentControl, and catch the event MouseLeftButtonDown on the ContentControl. In the event handler do e.Handled = true; This way the ContextMenu would have no way of knowing that the user has clicked on the TabControl.


try to put your tabcontrol inside a menuItem like the example:

<ContextMenu>
<MenuItem StaysOpenOnClick="true">
<MenuItem.Header>
...your TabControl
</MenuItem.Header>
</MenuItem>
</ContextMenu>

I hope that it can be helpful.

0

精彩评论

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

关注公众号