首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListBox contextMenu SubItem单击

ListBox contextMenu SubItem单击
EN

Stack Overflow用户
提问于 2018-01-16 12:05:35
回答 1查看 185关注 0票数 1

我有ListBoxContextMenu,我已经将MenuItem配置为只有在选择的项中单击才能工作。

这是XAML代码:

代码语言:javascript
复制
<ListBox x:Name="MessagesLb" Grid.Column="1" Margin="241,100,22.4,50" Grid.Row="1" BorderThickness="0" FontSize="14" FontWeight="SemiBold" ItemsSource="{Binding Items}" SelectionMode="Extended">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />
                                <Separator/>
                                <MenuItem Header="Dettagli" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="0">
                            <Setter Property="ContextMenu" Value="{x:Null}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我试图在我的Click中添加MenuItem事件,但是它不工作。

示例:

代码语言:javascript
复制
<MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />

代码语言:javascript
复制
private void MessagesLbCopySubMi_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Click event done");
}

我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-16 12:32:28

ContextMenu定义为资源:

代码语言:javascript
复制
<ListBox x:Name="MessagesLb" Grid.Column="1" Margin="241,100,22.4,50" Grid.Row="1" BorderThickness="0" FontSize="14" FontWeight="SemiBold" 
         ItemsSource="{Binding Items}" SelectionMode="Extended">
    <ListBox.Resources>
        <ContextMenu x:Key="cm">
            <MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />
            <Separator/>
            <MenuItem Header="Dettagli" />
        </ContextMenu>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContextMenu" Value="{StaticResource cm}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="0">
                    <Setter Property="ContextMenu" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48280884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档