RoutedCommand提供默认图像

时间:2015-03-04 15:14:34

标签: wpf

RoutedUICommand相比,

RoutedCommand提供了额外的Text属性。菜单项,按钮等会自动使用此Text

是否有RoutedCommand提供了菜单项,按钮栏等使用的默认图片或图标?如果没有,我该如何创建一个?

1 个答案:

答案 0 :(得分:3)

当然你知道如何扩展RoutedUICommand,我写了这样的东西:

public class ExtendedRoutedUICommand : RoutedUICommand
{
    private ImageSource imageSource;

    public ImageSource ImageSource
    {
        get
        {
            return imageSource;
        }
        set
        {
            imageSource = value;
        }
    }
}

现在您需要创建一个知道自己ExtendedRoutedUICommand的控件。例如,我创建了这个:

public class ExtendedMenuItem : System.Windows.Controls.MenuItem
{
    static ExtendedMenuItem()
    {
        System.Windows.Controls.MenuItem.IconProperty.OverrideMetadata(typeof(ExtendedMenuItem),
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(ExtendedMenuItem.CoerceIcon)));
    }

    public ExtendedMenuItem()
    {
        DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(MenuItem.CommandProperty, GetType());
        dpd.AddValueChanged(this, OnCommandChanged);
    }

    private static object CoerceIcon(DependencyObject d, object value)
    {
        ExtendedMenuItem menuItem = (ExtendedMenuItem)d;
        ExtendedRoutedUICommand extendedRoutedUICommand;
        if (value == null)
        {
            extendedRoutedUICommand = (menuItem.Command as ExtendedRoutedUICommand);
            if (extendedRoutedUICommand != null)
            {
                value = extendedRoutedUICommand.ImageSource;
            }

            return value;
        }

        return value;
    }

    private void OnCommandChanged(object sender, EventArgs args)
    {
        CoerceValue(MenuItem.IconProperty);
    }
}

现在问题是MenuItem的默认模板。您可以找到示例here。您只需要使用ContentPresenter控件替换Icon的Image。所以正确的风格是:

<Style x:Key="{x:Type local:ExtendedMenuItem}" TargetType="{x:Type local:ExtendedMenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ExtendedMenuItem}">
                <Border Name="Border">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
                            <ColumnDefinition Width="13"/>
                        </Grid.ColumnDefinitions>
                        <Image 
    Name="Icon"
    Margin="6,0,6,0"
    VerticalAlignment="Center"
    Source="{TemplateBinding Icon}" Stretch="Uniform" MaxHeight="16" MaxWidth="16" />
                        <Border 
    Name="Check"  
    Width="13" Height="13" 
    Visibility="Collapsed"
    Margin="6,0,6,0" 
    Background="{StaticResource NormalBrush}"
    BorderThickness="1"
    BorderBrush="{StaticResource NormalBorderBrush}">
                            <Path 
        Name="CheckMark"
        Width="7" Height="7" 
        Visibility="Hidden" 
        SnapsToDevicePixels="False" 
        Stroke="{StaticResource GlyphBrush}"
        StrokeThickness="2"
        Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                        </Border>
                        <ContentPresenter 
    Name="HeaderHost"
    Grid.Column="1"
    ContentSource="Header" VerticalAlignment="Center"
    RecognizesAccessKey="True"/>
                        <TextBlock x:Name="InputGestureText" VerticalAlignment="Center"
    Grid.Column="2"
    Text="{TemplateBinding InputGestureText}"
    Margin="5,2,0,2"
    DockPanel.Dock="Right" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="Icon" Value="{x:Null}">
                        <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsCheckable" Value="true">
                        <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="Background"
            Value="{StaticResource SelectedBackgroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我希望它可以帮到你。