簡體   English   中英

無法更改materialdesigninxaml的重音

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM