将未读通知添加到wpf microsoft ribbonbutton图像

时间:2014-09-02 12:42:03

标签: wpf ribbon-control

我想提出与Coxy相同的问题:我想在 RibbonButton 的图像上显示未读项目的数量。

示例:

Like this

我可以不修改整个模板的副本吗?我再问同样问题的原因是我使用的是Microsoft Ribbon控件和Coxy问题提供的解决方案,只能应用于Telerik的Ribbon控件。

2 个答案:

答案 0 :(得分:1)

我在答案中添加此内容,因为它的评论时间太长。

我去了微软下载网站&从here下载了Ribbon控件的源代码。包含所有触发器的完整模板就在那里。我不会在这里粘贴任何版本,因为它受版权保护,但任何人都可以下载并安装它。我想。

安装程序在C:\ Program Files(x86)\ MicrosoftRibbonForWPF文件夹中安装源文件的zip文件。我将zip文件解压缩到VS项目文件夹中。

我正在查看的模板位于v4.0 \ Themes \ Generic.xaml文件中。

那些坏的触发器确实引用了同一个属性。它是HighContrast静态对象的SystemParameters2.Current属性。

您可以为您的应用程序组合一个甚至不使用这些触发器的自定义模板。至少,这就是我要做的事情。

答案 1 :(得分:0)

Martin Andersen是对的:目前无法在XAML中创建功能区按钮,在“文档大纲”窗口中右键单击它,选择“编辑模板”并创建它的副本。 VS将显示以下错误消息,并且不生成任何模板:

Visual Studio error occurring when creating a copy of the ribbon button control template

如果在Blend中执行相同的操作,则会出现相同的错误消息,但实际上会生成功能区按钮的控件模板。不幸的是,这个模板是错误的。它包含几个触发器,其绑定绑定到(0)。以下是这种错误触发器的示例。

<DataTrigger Binding="{Binding (0)}" Value="True">
    <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
    <Setter Property="Background" TargetName="OuterBorder" Value="Transparent"/>
    <Setter Property="BorderBrush" TargetName="OuterBorder" Value="Transparent"/>
    <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
</DataTrigger>

此问题也向Microsoft报告:https://connect.microsoft.com/VisualStudio/feedback/details/794891/problems-styling-wpf-4-5-ribbon-control 它们提供默认控件模板作为此问题的附件,但遗憾的是我无法下载它们。生成的文件始终为空(零千字节)。有人可以下载吗?

提议的解决方案

我目前的建议是将按钮封装在另一个源自ContentControl的自定义控件中,并处理所有通知气泡。然后,您可以将按钮设置为此新控件的内容。我目前正在研究一个例子 - 当我找到有用的东西时,我会更新这篇文章。

错误的控制模板

以下是RibbonButton的完整错误控制模板:

<ControlTemplate x:Key="RibbonButtonWithNotificationStyle" TargetType="{x:Type RibbonButton}">
    <Border x:Name="OuterBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}" SnapsToDevicePixels="True">
        <Border x:Name="InnerBorder" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" Padding="{TemplateBinding Padding}">
            <StackPanel x:Name="StackPanel">
                <Image x:Name="PART_Image" RenderOptions.BitmapScalingMode="NearestNeighbor" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Height="32" Margin="{DynamicResource {ComponentResourceKey ResourceId=LargeImageMargin, TypeInTargetAssembly={x:Type Ribbon}}}" Source="{TemplateBinding LargeImageSource}" VerticalAlignment="Center" Width="32"/>
                <Grid x:Name="Grid" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <RibbonTwoLineText x:Name="TwoLineText" HorizontalAlignment="Center" LineStackingStrategy="BlockLineHeight" LineHeight="13" Margin="1,1,1,0" TextAlignment="Center" Text="{TemplateBinding Label}" VerticalAlignment="Top"/>
                </Grid>
            </StackPanel>
        </Border>
    </Border>
    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Self}}" Value="Large">
            <Setter Property="MinWidth" Value="44"/>
            <Setter Property="Height" Value="66"/>
            <Setter Property="MinHeight" TargetName="Grid" Value="26"/>
            <Setter Property="RibbonTwoLineText.HasTwoLines" TargetName="TwoLineText" Value="True"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Self}}" Value="Small">
            <Setter Property="Height" Value="22"/>
            <Setter Property="Margin" TargetName="PART_Image" Value="1,0"/>
            <Setter Property="Source" TargetName="PART_Image" Value="{Binding SmallImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="Width" TargetName="PART_Image" Value="16"/>
            <Setter Property="Height" TargetName="PART_Image" Value="16"/>
            <Setter Property="HorizontalAlignment" TargetName="TwoLineText" Value="Left"/>
            <Setter Property="Margin" TargetName="TwoLineText" Value="1"/>
            <Setter Property="Orientation" TargetName="StackPanel" Value="Horizontal"/>
        </DataTrigger>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Self}}" Value="Small"/>
                <Condition Binding="{Binding IsInQuickAccessToolBar, RelativeSource={RelativeSource Self}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Height" Value="Auto"/>
        </MultiDataTrigger>
        <DataTrigger Binding="{Binding ControlSizeDefinition.IsLabelVisible, RelativeSource={RelativeSource Self}}" Value="False">
            <Setter Property="Visibility" TargetName="TwoLineText" Value="Collapsed"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Self}}" Value="Collapsed">
            <Setter Property="Visibility" TargetName="PART_Image" Value="Collapsed"/>
        </DataTrigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" TargetName="OuterBorder" Value="{Binding MouseOverBackground, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{Binding MouseOverBorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="True">
            <Setter Property="Background" TargetName="OuterBorder" Value="{Binding FocusedBackground, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{Binding FocusedBorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" TargetName="OuterBorder" Value="{Binding PressedBackground, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{Binding PressedBorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="Transparent"/>
        </Trigger>
        <Trigger Property="IsInControlGroup" Value="True">
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{Binding Ribbon.BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
            <Setter Property="BorderThickness" TargetName="OuterBorder" Value="0,0,1,0"/>
            <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
            <Setter Property="CornerRadius" TargetName="InnerBorder" Value="0"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" TargetName="PART_Image" Value="0.5"/>
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="#FF9E9E9E"/>
        </Trigger>
        <DataTrigger Binding="{Binding (0)}" Value="True">
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
            <Setter Property="Background" TargetName="OuterBorder" Value="Transparent"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="Transparent"/>
            <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="False"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDropDownOpen, FallbackValue=false, RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="False"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
            <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="Transparent"/>
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
            <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="Transparent"/>
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
            <Setter Property="CornerRadius" TargetName="OuterBorder" Value="0"/>
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsInControlGroup, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="False"/>
                <Condition Binding="{Binding (0)}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </MultiDataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>