[英]Cannot change accent on materialdesigninxaml
我正在尝试将MaterialDesign重音添加到MenuItem,实际上我已经部分管理了此操作,这是我到目前为止所做的:
菜单创建:
<Menu Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Stretch">
<MenuItem Header="Stile" Width="100"
ItemContainerStyle="{StaticResource AccentColorMenuItemStyle}"
ItemsSource="{Binding SettingsController.Swatches, Mode=OneWay}" />
</Menu>
容器定义:
<Ellipse x:Key="AccentMenuIcon"
Width="16"
Height="16"
x:Shared="False"
Fill="{Binding AccentExemplarHue.Color, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}" />
<Style x:Key="AccentColorMenuItemStyle"
BasedOn="{StaticResource MetroMenuItem}" TargetType="{x:Type MenuItem}">
<Setter Property="Command" Value="{Binding DataContext.ApplyAccentCommand,
RelativeSource={RelativeSource AncestorType=Window}}" />
<Setter Property="Header" Value="{Binding Name, Mode=OneWay}" />
<Setter Property="Icon" Value="{StaticResource AccentMenuIcon}" />
</Style>
我定义了一个颜色转换器以显示为椭圆颜色:
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new SolidColorBrush((Color)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush c = (SolidColorBrush)value;
System.Drawing.Color col = System.Drawing.Color.FromArgb(c.Color.A, c.Color.R, c.Color.G, c.Color.B);
return col;
}
}
然后在我的viewmodel中,我是这样实现的:
private static void ApplyBase(bool isDark)
{
new PaletteHelper().SetLightDark(isDark);
}
public IEnumerable<Swatch> Swatches { get; }
public ICommand ApplyPrimaryCommand { get; } = new SimpleCommand(o => ApplyPrimary((Swatch)o));
private static void ApplyPrimary(Swatch swatch)
{
new PaletteHelper().ReplacePrimaryColor(swatch);
}
public ICommand ApplyAccentCommand { get; } = new SimpleCommand(o => ApplyAccent((Swatch)o));
private static void ApplyAccent(Swatch swatch)
{
new PaletteHelper().ReplaceAccentColor(swatch);
}
使用上面的代码,我将所有重音符号显示在menuItem中,但是当出现问题时
我单击menuItem颜色,然后调用命令“ ApplyAccentCommand”,在这里得到一个空异常:
private static void ApplyAccent(Swatch swatch)
{
new PaletteHelper().ReplaceAccentColor(swatch);
}
尤其是在色板对象(就是口音)上,我做错了什么? 谢谢。
我怀疑您实际上正在获取ArgumentNullException。 由于ReplaceAccentColor检查传入的参数是否为null 。 如果您在该行上设置断点,我怀疑swatch为空。
我看到您正在设置命令,但未设置命令的参数,这意味着将使用默认值null。
只需在菜单项样式中添加一个设置器即可解决。
<Setter Property="CommandParameter" Value="{Binding}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.