从按钮单击打开ContextMenu

时间:2014-09-11 23:32:26

标签: wpf button click contextmenu

我正在尝试创建一个按钮,在单击时显示弹出窗口中的内容。所以我提出了这样简单的事情:

public class DropdownButton : Button
{
    public object DropdownContent
    {
        get { return (object)GetValue(DropdownContentProperty); }
        set { SetValue(DropdownContentProperty, value); }
    }

    public static readonly DependencyProperty DropdownContentProperty =
        DependencyProperty.Register("DropdownContent", typeof(object), typeof(DropdownButton), new PropertyMetadata(null));

    protected override void OnClick()
    {
        base.OnClick();

        if(DropdownContent != null)
        {
            if(DropdownContent is ContextMenu)
            {
                ContextMenu = (ContextMenu)DropdownContent;
                ContextMenu.IsOpen = true;
            }
            else
            {
                // launch a popup
            }
        }
    }
}

我正在使用它:

public partial class MainWindow : Window
{
    public string A { get { return "A"; } }
    public string B { get { return "B"; } }

    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }
}

<Window x:Class="WpfApplication45.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:WpfApplication45"
    Title="MainWindow"
    WindowStartupLocation="CenterScreen">
<Window.Resources>
    <ContextMenu x:Key="sharedMenu">
        <MenuItem Header="{Binding}" />
    </ContextMenu>
</Window.Resources>

<StackPanel>
    <UniformGrid Columns="2"
                 DataContext="{Binding A}">
        <TextBlock Text="{Binding}"
                   Background="#CCC"
                   ContextMenu="{StaticResource sharedMenu}"/>
        <l:DropdownButton DropdownContent="{StaticResource sharedMenu}" />
    </UniformGrid>

    <UniformGrid Columns="2"
                 DataContext="{Binding B}">
        <TextBlock Text="{Binding}" 
                   Background="#CCC"
                   ContextMenu="{StaticResource sharedMenu}"/>
        <l:DropdownButton DropdownContent="{StaticResource sharedMenu}" />
    </UniformGrid>
</StackPanel>

但它没有达到预期的效果。如果您尝试单击按钮,则会弹出ContextMenu,但不会设置DataContext。

如果我像这样手动设置DataContext:

ContextMenu = (ContextMenu)DropdownContent;
                ContextMenu.DataContext = DataContext;
                ContextMenu.IsOpen = true;

它打破了TextBlocks ContextMenu上的绑定

1 个答案:

答案 0 :(得分:0)

说实话,我不知道为什么会有效,但如果我应用以下更改:

 protected override void OnClick()
    {
        base.OnClick();

        if (DropdownContent != null)
        {
            if (DropdownContent is ContextMenu)
            {
                ContextMenu = (ContextMenu)DropdownContent;
                ContextMenu.PlacementTarget = this;
                ContextMenu.IsOpen = true;
            }
            else
            {
                // launch a popup
            }
        }
    }

除了禁用菜单动画:

  <Application.Resources>
    <PopupAnimation x:Key="{x:Static SystemParameters.MenuPopupAnimationKey}">None</PopupAnimation>
</Application.Resources>

事情有效......