更改ScottPlot中右键菜单的背景颜色
在C#中引用ScottPlot.WPF(5.0.55)
1.尝试在代码中使用ScottPlot中开放出的属性修改Menu的背景色(失败)
没有开放Menu的background,修改ConetxtMenu无效,跟进发现这里的菜单项赋值在Menu属性中而非ContextMenu
2.尝试在XAML中修改background(失败)
XAML
<scottplot:WpfPlot x:Name="sct_DataPlot" Width="1050" Height="200" Background="Beige" >
<scottplot:WpfPlot.ContextMenu>
<ContextMenu>
<ContextMenu.Style>
<Style TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource {x:Type ContextMenu} }">
<Setter Property="Background" Value="Beige"/>
</Style>
</ContextMenu.Style>
</ContextMenu>
</scottplot:WpfPlot.ContextMenu>
</scottplot:WpfPlot>
显示出来依然是灰色

验证这里对ContextMenu的设置是否有效
查看代码
<scottplot:WpfPlot x:Name="sct_DataPlot" Width="1050" Height="200" Background="Beige" >
<scottplot:WpfPlot.ContextMenu>
<ContextMenu>
<MenuItem Header="保存图片" Click="SaveImage_Click" Background="Beige"/>
</ContextMenu>
</scottplot:WpfPlot.ContextMenu>
</scottplot:WpfPlot>
发现这里的MenuItem的赋值无效,与1中结论相同,表明需要对WpfPlot.Menu进行设置,但是没有这个入口
3.使用全局资源App.xaml
查看代码
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#D2B48C" SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml">
</ResourceDictionary>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign3.Defaults.xaml">
</ResourceDictionary>
<ResourceDictionary>
<SolidColorBrush x:Key="Menu.Static.Background" Color="Beige"/>
<!-- 静态背景色 -->
<SolidColorBrush x:Key="MenuItem.Highlight.Background" Color="#3D26A0DA"/>
<!-- 鼠标悬停背景色 -->
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="Background" Value="{StaticResource Menu.Static.Background}"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这里明显修改颜色成功,但是左侧有色块

应该是颜色设置成功,但与其他原生属性冲突
增加BasedOn属性,防止冲突
<Style TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource {x:Type ContextMenu}}">
问题解决

思考:使用全局资源可以修改成功,是否在2中是定义层级过低,尝试向上一层再次设置ContextMenu
在windows资源中修改效果如下,但无法添加BasedOn ContextMenu


之后没有做什么其他修改,但是很奇怪,可以运行了,后发现蓝色波浪线可以无视,直接运行就好
在xaml中的windowsResourse中添加
<Window.Resources>
<Style TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource {x:Type ContextMenu}}">
<Setter Property="Background" Value="Beige"/>
</Style>
</Window.Resources>
问题解决~

浙公网安备 33010602011771号