WPF 自定义ContextMenu且为左键点击显示

 1<Button Click="Button_Click_3"  Style="{StaticResource NormalButtonStyle}">
 2  <Canvas>
 3    <Line X1="-5" Y1="-4" X2="5" Y2="-4" Fill="White" Stroke="White" StrokeThickness="1"/>
 4    <Line X1="-5" Y1="-4" X2="-5" Y2="4" Fill="White" Stroke="White" StrokeThickness="1"/>
 5    <Line X1="-5" Y1="4" X2="5" Y2="4" Fill="White" Stroke="White" StrokeThickness="1"/>
 6    <Line X1="5" Y1="-4" X2="5" Y2="4" Fill="White" Stroke="White" StrokeThickness="1"/>
 7  Canvas>
 8  <Button.ContextMenu>
 9    <ContextMenu Name="menu" Placement="MousePoint" Width="200">
10      <MenuItem Header="Item1"/>
11      <MenuItem Header="Item2"/>
12      <MenuItem Header="Item3"/>
13      <MenuItem Header="Item4"/>
24    ContextMenu>
25  Button.ContextMenu>
26Button>


上为XAML设计代码,Style部分是Button的样式。
Canvas内的代码是Line,图案是一个方框。怎么修改可自行百度

左键显示Menu代码如下
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //获取或设置一个值,该值指示 System.Windows.Controls.ContextMenu 是否可见。
            this.menu.IsOpen = true;
        }
 
    

 

 

 

转载于:https://www.cnblogs.com/SunsetAzure/p/4711511.html

你可能感兴趣的:(WPF 自定义ContextMenu且为左键点击显示)