200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > c# WPF listbox右键鼠标处弹出快捷菜单

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

时间:2023-03-07 07:36:21

相关推荐

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。