._wpf 自定义 menu">
当前位置:   article > 正文

WPF一个简单的垂直菜单样式的实现_wpf 自定义 menu

wpf 自定义 menu
  1. <Style TargetType="{x:Type Menu}">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="{x:Type Menu}">
  5. <Border CornerRadius="0">
  6. <ItemsPresenter/>
  7. </Border>
  8. </ControlTemplate>
  9. </Setter.Value>
  10. </Setter>
  11. </Style>
  12. <Style TargetType="{x:Type MenuItem}">
  13. <Setter Property="Template">
  14. <Setter.Value>
  15. <ControlTemplate TargetType="{x:Type MenuItem}">
  16. <Border Margin="0,2" x:Name="bd" Background="#CCCCCC" MinWidth="100" CornerRadius="0">
  17. <Grid>
  18. <Grid.ColumnDefinitions>
  19. <ColumnDefinition Width="30"/>
  20. <ColumnDefinition/>
  21. </Grid.ColumnDefinitions>
  22. <Rectangle Fill="#99CC66" Height="20" Width="20"/>
  23. <ContentPresenter Margin="0,10" ContentSource="Header" Grid.Column="1" HorizontalAlignment="Left"/>
  24. <Popup x:Name="pop" Margin="10" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Placement="Right">
  25. <Border Margin="5" CornerRadius="5" Background="#FFCCCC">
  26. <StackPanel IsItemsHost="True">
  27. </StackPanel>
  28. </Border>
  29. </Popup>
  30. </Grid>
  31. </Border>
  32. <ControlTemplate.Triggers>
  33. <Trigger Property="IsMouseOver" Value="true">
  34. <Setter TargetName="bd" Property="Background" Value="#0099CC"/>
  35. <Setter TargetName="pop" Property="IsOpen" Value="true"/>
  36. </Trigger>
  37. <Trigger Property="IsMouseOver" Value="False">
  38. <Setter TargetName="pop" Property="IsOpen" Value="false"/>
  39. </Trigger>
  40. <Trigger Property="IsPressed" Value="True">
  41. <Setter TargetName="bd" Property="Background" Value="#FF6666"/>
  42. <Setter TargetName="pop" Property="IsOpen" Value="false"/>
  43. </Trigger>
  44. </ControlTemplate.Triggers>
  45. </ControlTemplate>
  46. </Setter.Value>
  47. </Setter>
  48. </Style>
  1. <StackPanel Width="100" HorizontalAlignment="Left">
  2. <Menu>
  3. <MenuItem Header="开始">
  4. <MenuItem Header="开始"></MenuItem>
  5. <MenuItem Header="开始"></MenuItem>
  6. <MenuItem Header="开始"></MenuItem>
  7. <MenuItem Header="开始"></MenuItem>
  8. </MenuItem>
  9. </Menu>
  10. <Menu>
  11. <MenuItem Header="哈哈">
  12. <MenuItem Header="哈哈"></MenuItem>
  13. <MenuItem Header="哈哈"></MenuItem>
  14. <MenuItem Header="哈哈"></MenuItem>
  15. <MenuItem Header="哈哈"></MenuItem>
  16. </MenuItem>
  17. </Menu>
  18. <Menu>
  19. <MenuItem Header="哦哦">
  20. <MenuItem Header="好看啦啦啦"></MenuItem>
  21. <MenuItem Header="啦啦啦"></MenuItem>
  22. <MenuItem Header="啦啦啦"></MenuItem>
  23. <MenuItem Header="啦啦啦"></MenuItem>
  24. </MenuItem>
  25. </Menu>
  26. </StackPanel>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/758456
推荐阅读
相关标签