日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2024/3/24 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;


? ? ? ? }

?

?

?

?

總結

以上是生活随笔為你收集整理的c# WPF listbox右键鼠标处弹出快捷菜单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。