【问题标题】:Opening a ContextMenu from a button click通过单击按钮打开 ContextMenu
【发布时间】:2014-11-06 01:21:36
【问题描述】:

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

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 上的绑定

【问题讨论】:

    标签: wpf button click contextmenu


    【解决方案1】:

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

     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>
    

    一切正常……

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多