开发者

Rounded Rect for Context Menu in WPF

I have a horizontal stackpanel that has a custom button, that when clicked, opens a context menu to the right of the button. I'd like that context menu to have a rounded rect. Is tehre a way to do that? I t开发者_高级运维hought I could wrap the in a element, but ContextMenu doesn't respond to Border. Thanks.


You will probably need to Style the ContextMenu element like so:

<Style TargetType="ContextMenu">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContextMenu">
                <Border CornerRadius="5" BorderBrush="Black" BorderThickness="1" Background="Blue" SnapsToDevicePixels="True">
                    <ItemsPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage:

ContextMenu m = new ContextMenu();

m.Items.Add("Item 1");
m.Items.Add("Item 2");
m.Items.Add("Item 3");

m.PlacementTarget = sender as UIElement;
m.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
m.IsOpen = true;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜