简体   繁体   English

为什么 WPF 用户控件的上下文菜单仅显示用户控件内的特定位置?

[英]why WPF context menu of a user control only show up particular places within the user control?

I m working on a noteBook project and i noticed one annoying behavior of context menu.我正在做一个笔记本项目,我注意到上下文菜单的一个烦人的行为。

I have a user control in which i defined three textblocks.我有一个用户控件,我在其中定义了三个文本块。 here is the code:这是代码:

<UserControl x:Class="NoteBookPractice.View.Controls.NoteControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:NoteBookPractice.View"
         mc:Ignorable="d" 
         d:DesignHeight="75" d:DesignWidth="400">
<StackPanel>
    <TextBlock Text="{Binding Title}"
               Margin="2"
               VerticalAlignment="Center"
               FontSize="13"
               FontWeight="Bold"
               Foreground="White"/>
    <TextBlock Text="{Binding CreatedAt}"
               Margin="2"
               VerticalAlignment="Center"
               FontSize="10"
               FontWeight="Light"
               Foreground="White"/>
    <TextBlock Text="{Binding UpdatedAt}"
               Margin="2"
               VerticalAlignment="Center"
               FontSize="10"
               FontWeight="Light"
               Foreground="White"/>
</StackPanel>

and here is the code in my window to use this user control:这是我的 window 中使用此用户控件的代码:

<ListView DockPanel.Dock="Left"
              Background="DarkCyan"
              Width="150"
              ItemsSource="{Binding Notes}"
              SelectedValue="{Binding SelectedNote}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <uc:NoteControl Note="{Binding}">
                    <uc:NoteControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Rename"
                                  Command="{Binding Source={StaticResource vm}, Path=RenameNoteCommand}"
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="Delete"
                                  Command="{Binding Source={StaticResource vm}, Path=DeleteNoteCommand}"
                                  CommandParameter="{Binding}"/>
                    </ContextMenu>
                    </uc:NoteControl.ContextMenu>
                </uc:NoteControl>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>

everything works great.一切都很好。 But i noticed the context menu will note show when the user clicks places other than the text inside the user control.. see image below:但我注意到上下文菜单会在用户单击用户控件内文本以外的位置时显示。请参见下图:

单击空白区域不显示上下文菜单

只有点击文本才会显示它

This is annoying because i thought the binding was not working.这很烦人,因为我认为绑定不起作用。 But then i clicked another place and it showed up.但后来我点击了另一个地方,它出现了。 I guess a user will also be confused我想用户也会感到困惑

is there any way i can make the context menu to show up wherever the user clicked on the note user control?有什么方法可以让上下文菜单显示在用户单击注释用户控件的任何位置?

After a lot of search.. i found a way to do this.经过大量搜索..我找到了一种方法来做到这一点。 Adding this code to the listview body将此代码添加到列表视图主体

<ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>

applying horizontalContentAlignment to user control directly or any of its parents seemed to be not working.直接将 HorizontalContentAlignment 应用于用户控件或其任何父控件似乎都不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM