第一个LeftClick上不显示上下文菜单

时间:2013-03-26 08:54:28

标签: wpf xaml contextmenu code-behind

我有以下几点:

<telerik:RadButton Name="AddButton" DockPanel.Dock="Right"
                   Content="Add" HorizontalAlignment="Right"
                   Click="AddButton_OnClick" Padding="5,0,5,0">
                                            <telerik:RadButton.ContextMenu>
                                                <ContextMenu x:Name="addContextMenu"
                                                    ItemsSource="{Binding Path=AvailableTypesToAdd}">
                                                    <ContextMenu.ItemTemplate>
                                                        <DataTemplate>
                                                            <MenuItem Header="{Binding Path=Name}"
                                                                      Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadExpander}}, Path=DataContext.AddCommand}"
                                                                      CommandParameter="{Binding}" />
                                                        </DataTemplate>
                                                    </ContextMenu.ItemTemplate>
                                                </ContextMenu>
                                            </telerik:RadButton.ContextMenu

在后面的代码中,我有以下内容:

private void AddButton_OnClick(object sender, RoutedEventArgs e)
    {
        AddButton.ContextMenu.PlacementTarget = this;
        AddButton.ContextMenu.IsOpen = true;
    }

但是,当我单击LeftClick时,上下文菜单为空,但是如果我右键单击它就会填充。如果我删除了PlacementTarget = this,如果我首先右键单击,那么当我左键单击时它将被填充。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我找到了修复程序。而不是设置:

AddButton.ContextMenu.PlacementTarget = this;

设置为:

AddButton.ContextMenu.PlacementTarget = (UIElement)sender;