简体   繁体   English

无法更改materialdesigninxaml的重音

[英]Cannot change accent on materialdesigninxaml

I'm trying to add the MaterialDesign accents to a MenuItem, actually I've managed this partially, this is what I did so far: 我正在尝试将MaterialDesign重音添加到MenuItem,实际上我已经部分管理了此操作,这是我到目前为止所做的:

Menu creation: 菜单创建:

 <Menu Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Stretch">
            <MenuItem Header="Stile" Width="100" 
                      ItemContainerStyle="{StaticResource AccentColorMenuItemStyle}"
                      ItemsSource="{Binding SettingsController.Swatches, Mode=OneWay}" />
 </Menu>

container definition: 容器定义:

 <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>

I defined a Color converter to display as Ellipse color: 我定义了一个颜色转换器以显示为椭圆颜色:

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;
    }
}

then in my viewmodel I've implemented in this way: 然后在我的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);
    }

with the code above I get all the accents displayed in the menuItem, but I've a problem, when 使用上面的代码,我将所有重音符号显示在menuItem中,但是当出现问题时

I click on the menuItem color, and the Command "ApplyAccentCommand" is called I get a null exception here: 我单击menuItem颜色,然后调用命令“ ApplyAccentCommand”,在这里得到一个空异常:

private static void ApplyAccent(Swatch swatch)
{
    new PaletteHelper().ReplaceAccentColor(swatch);
}

in particular on the swatch object (that's the accent) what I did wrong? 尤其是在色板对象(就是口音)上,我做错了什么? Thanks. 谢谢。

I suspect you are actually getting an ArgumentNullException. 我怀疑您实际上正在获取ArgumentNullException。 Since the ReplaceAccentColor checks the incoming parameter for null . 由于ReplaceAccentColor检查传入的参数是否为null If you set a break point on that line I suspect that swatch is null. 如果您在该行上设置断点,我怀疑swatch为空。

I see that you are setting up the command, but not setting up the command's parameter meaning that the default value of null will be used. 我看到您正在设置命令,但未设置命令的参数,这意味着将使用默认值null。

Simply adding one more setter to your menu item style should fix it. 只需在菜单项样式中添加一个设置器即可解决。

<Setter Property="CommandParameter" Value="{Binding}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM