c# WPF listbox右键鼠标处弹出快捷菜单

原创    右键鼠标在item上就弹出快捷菜单,空白处不弹出.

 

这个直接添加一个Item模板就行了

MouseRightButtonUp="listBox_MouseRightButtonDown"

   <ListBox x:Name="listBox" IsSynchronizedWithCurrentItem="True" Margin=" 0,5,0,0"  BorderThickness="1" BorderBrush="#FF708AB9" HorizontalAlignment="Left" Width="202" Background="{x:Null}" RenderTransformOrigin="0.5,0.5" Height="228">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid  >
                            <Rectangle Width="20" Height="20" Fill="#FF6DE493" HorizontalAlignment="Left"   /><!--这个试验品-->
                            <TextBlock Text="{Binding}" MouseRightButtonUp="listBox_MouseRightButtonDown"

Margin="20,0,0,0" ToolTip="{Binding}">

                          </TextBlock>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>

 

 

 

//下面这个是我之前搞的.

这个方法我真的搞了好久,c# winfrom就不贴了,网上有很多.

 

 <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="2,0,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">

<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" MouseRightButtonUp="Bd_MouseRightButtonUp_1">

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsSelected" Value="true"/> <Condition Property="Selector.IsSelectionActive" Value="false"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/> </MultiTrigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>这里一定要用

MouseRightButtonUp

用 MouseRightButtonDown没有用. 不知道为什么.谁知道告诉我一声.毕竟我是个业余的.

后台代码

 private void Bd_MouseRightButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            ContextMenu aMenu = new ContextMenu();
            MenuItem scMenu = new MenuItem();
            scMenu.Header = "删除";
            scMenu.Click += sc_Click;
            aMenu.Items.Add(scMenu);
            MenuItem fzMenu = new MenuItem();
            fzMenu.Header = "复制";
            fzMenu.Click += fz_Click;
            aMenu.Items.Add(fzMenu);
            MenuItem ztMenu = new MenuItem();
            ztMenu.Header = "粘贴";
            ztMenu.Click += zt_Click;
            aMenu.Items.Add(ztMenu);
            listBox.ContextMenu = aMenu.ContextMenu;
            MenuItem qkMenu = new MenuItem();
            qkMenu.Header = "清空";
            qkMenu.Click += QkMenu_Click;
            aMenu.Items.Add(qkMenu);
            aMenu.IsOpen = true;
            ListBox lb = new ListBox();
            lb = listBox;
            if (lb.SelectedItem==null)//这个判断不能少,不然会六脉神剑的.一般是好的,但是有时候会出错.我也不知道为什么.
            {
                return;
            }
            a = lb.SelectedItem.ToString();




            //  listBox.ContextMenu .IsOpen = true;


        }

 

 

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值