开发者

WPF Button with context menu, How to bind context menu width to width of the button?

开发者 https://www.devze.com 2023-02-14 17:17 出处:网络
I\'ve got a simple button where I want the context menu\'s width to be the same width as the button (I\'m making the left click of the button open the context menu directly below the button).

I've got a simple button where I want the context menu's width to be the same width as the button (I'm making the left click of the button open the context menu directly below the button).

<Button x:Name="btn" Content="Push Me">
    <Button.ContextMenu>
        <ContextMenu x:Name="cm">
      开发者_JAVA百科      <MenuItem Header="One" />
            <MenuItem Header="Two" />
            <MenuItem Header="Three" />
        </ContextMenu>
    </Button.ContextMenu>
</Button>

I've tried the following binding on the context menu itself and it doesn't work

<ContextMenu x:Name="cm" Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}, Path=ActualWidth}">

But, I've been able to get it working in code

btn.LayoutUpdated += (s, e) => cm.Width = btn.ActualWidth;

My question is... Does a xaml binding exist that will get me this functionality?


ContextMenu has a Property called PlacementTarget which in this case will be the Button so you can try to use in the Path for the Binding

<ContextMenu x:Name="cm"
             Width="{Binding RelativeSource={RelativeSource Self},
                             Path=PlacementTarget.ActualWidth}">


This has to do with the way in which the ContextMenu behaves with regard to its NameScope. If you analyze the ContextMenu in the debugger you willl note its parent is a Popup and the parent of the Popup is null.

One of the workarounds noted on the MSDN forums is to adjust the NameScope which will then allow the ContextMenu to behave as you would expect.

    NameScope.SetNameScope(this, new NameScope());

This is obviously going to take place in the code behind as is your other approach; neither being optimal.

0

精彩评论

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

关注公众号