通用工业视觉软件设计之流程栏-TreeView-右键模块菜单栏

一 iconfont下载图像
禁用

二 菜单栏样式
<!--菜单外部样式-->
<Style TargetType="{x:Type ContextMenu}" x:Key="ContextMu">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContextMenu">
<!--背景色-->
<Border Name="bd" Background="Black" BorderThickness="1" BorderBrush="Gray" Width="160">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--中间横线-->
<Style x:Key="SeperatorTemplate" TargetType="Separator">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Separator">
<Border BorderBrush="White" BorderThickness="0 0 0 1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--菜单栏内部项样式-->
<Style x:Key="MenuItemTemplate" TargetType="MenuItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<Border Name="Bt" Height="32" Background="Transparent">
<Border Name="bd" Height="32" Background="Transparent">
<StackPanel Orientation="Horizontal">
<Image x:Name="img" Stretch="None" Margin="10,0,10,0" Source="{Binding Icon, RelativeSource={RelativeSource TemplatedParent}}"/>
<TextBlock x:Name="tb" Margin="0,0,10,0" Foreground="White" FontSize="13" VerticalAlignment="Center" Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"/>
</StackPanel>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Bt" Property="Background" Value="gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--菜单栏内部项样式1-->
<Style x:Key="MenuItemTemplate1" TargetType="MenuItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<Border Name="Bt" Height="18" Background="Transparent">
<Border Name="bd" Height="18" Background="Transparent">
<StackPanel Orientation="Horizontal">
<Image x:Name="img" Stretch="None" Margin="10,0,10,0" Source="{Binding Icon, RelativeSource={RelativeSource TemplatedParent}}"/>
<TextBlock x:Name="tb" Margin="0,0,10,0" Foreground="White" FontSize="13" VerticalAlignment="Center" Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"/>
</StackPanel>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
三 右键
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<StackPanel
MouseRightButtonUp="StackPanel_MouseRightButtonUp"
private void StackPanel_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right && SelectedNode != null)
{
ContextMenu cm = CreateContextMenu();
cm.PlacementTarget = sender as StackPanel;
cm.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
cm.IsOpen = true;
}
}
private ContextMenu CreateContextMenu()
{
ContextMenu aMenu = new ContextMenu();
aMenu.Style = (Style)this.FindResource("ContextMu");
Style Line = (Style)this.FindResource("SeperatorTemplate");
Style MenuItem1 = (Style)this.FindResource("MenuItemTemplate");
Style MenuItem2 = (Style)this.FindResource("MenuItemTemplate1");
MenuItem null1 = new MenuItem();
null1.Style = MenuItem2;
aMenu.Items.Add(null1);
MenuItem CopyMenu = new MenuItem();
CopyMenu.Style = MenuItem1;
CopyMenu.Header = "复制";
CopyMenu.Icon = "pack://application:,,,/ModuleView;component/07ComponentFrm/ImageMenu/复制.png";
aMenu.Items.Add(CopyMenu);
Separator sep1 = new Separator();
sep1.Style = Line;
aMenu.Items.Add(sep1);
MenuItem PasteMenu = new MenuItem();
PasteMenu.Style = MenuItem1;
PasteMenu.Header = "粘贴";
PasteMenu.Icon = "pack://application:,,,/ModuleView;component/07ComponentFrm/ImageMenu/粘贴.png";
aMenu.Items.Add(PasteMenu);
MenuItem DisableMenu = new MenuItem();
DisableMenu.Style = MenuItem1;
ModuleInfo module = ModuleInfoList.Find(c => c.ModuleName == SelectedNode.Name);
if (module != null)
{
DisableMenu.Header = module.IsUse ? "禁用模块" : "启用模块";
DisableMenu.Icon = module.IsUse ? "pack://application:,,,/ModuleView;component/07ComponentFrm/ImageMenu/禁用.png" :
"pack://application:,,,/ModuleView;component/07ComponentFrm/ImageMenu/启用.png";
}
aMenu.Items.Add(DisableMenu);
Separator sep2 = new Separator();
sep2.Style = Line;
aMenu.Items.Add(sep2);
MenuItem DeleteMenu = new MenuItem();
DeleteMenu.Style = MenuItem1;
DeleteMenu.Header = "删除模块";
DeleteMenu.Icon = "pack://application:,,,/ModuleView;component/07ComponentFrm/ImageMenu/删除.png";
aMenu.Items.Add(DeleteMenu);
MenuItem null2 = new MenuItem();
null2.Style = MenuItem2;
aMenu.Items.Add(null2);
return aMenu;
}