2010-01-11 84 views
5

我有一个自动生成每日和每周报告的C#WPF项目。我想通知当新报表可用的用户,所以我认为像iPhone上的徽章,其中出现一个小红圈新邮件的数量: alt text http://i46.tinypic.com/so3l2u.pngiPhone像WPF项目中的红色徽章通知?

我想到三个图像:两个图像如果要显示的数字很小,则在左侧和右侧有半个圆圈。中间的第三张图片表示数字很大(123),并且不适合放在一个圆圈内。 alt text http://i49.tinypic.com/11lr7mp.png

我想要一个光滑的效果,所以我想到了图片。有没有人有一个好主意如何做到这一点没有图片,但编程?

+0

徽章?徽章?我们不需要没有臭气的徽章! – jpierson 2011-07-14 15:56:50

回答

15

使用边框元素并将其放置在其中。您可以适当地设置边框的CornerRadius属性,使其看起来像一个圆形(或圆形矩形形状,以防数字较大)。

这里的首次下调,其利用了这一CornerRadius将获得钳位到高度或宽度的一半分别在Y和X:

<Border Background="Red" CornerRadius="999" Padding="4"> 
    <TextBlock Foreground="White" FontWeight="Bold" FontSize="12">125</TextBlock> 
</Border> 
+0

和一个渐变画笔,或者使用不同画笔的几个边框会使它变得“有光泽” – arconaut 2010-01-11 20:15:06

+0

可能要使其成为Adorner https://msdn.microsoft.com/en-us/library/ms743737(v=vs。 110).aspx – Kelly 2017-03-08 03:57:04

0

这是我去了,它并不完美,但它看起来不够好。

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Page.Resources> 
    <DropShadowEffect x:Key="ShadowEffect" Direction="270" BlurRadius="5" ShadowDepth="3"/> 
     <Style TargetType="Label" x:Key="CircularLabel"> 
      <Setter Property="Foreground" Value="White" /> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Setter Property="FontSize" Value="12" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Label"> 
         <Grid> 
          <Rectangle HorizontalAlignment="Center" VerticalAlignment="Center" Width="20" Height="20" Fill="#FFC90000" RadiusX="10" RadiusY="10" Stroke="White" StrokeThickness="2" Effect="{StaticResource ShadowEffect}" /> 
          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}"></ContentPresenter> 
          <Rectangle x:Name="TopShine" RadiusX="10" RadiusY="10" Width="20" Height="20" StrokeThickness="2"> 
           <Rectangle.Fill> 
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity="0.8"> 
             <GradientStop Color="White" Offset="0" /> 
             <GradientStop Color="Transparent" Offset="0.6" /> 
            </LinearGradientBrush> 
           </Rectangle.Fill> 
          </Rectangle> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Page.Resources> 
    <Grid> 
    <Label Style="{StaticResource CircularLabel}">1</Label> 
    </Grid> 
</Page> 
2

这是基于Chris1的答案,但是这将正确地舒展时候徽章中的文本长于一个数字,我还设置字体,使其跨Windows版本更加一致,改变了大小稍微补偿并在徽章周围添加轮廓。

我也换成DropShadowEffect用矩形,这是因为我不能在我的特定应用程序中使用DropShadowEffect,DropShadowEffect看起来更好,但我的矩形阴影是不够好,你可以删除阴影矩形,如果你使用DropShadowEffect喜欢。

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Page.Resources> 
    <Style TargetType="Label" x:Key="CircularLabel"> 
     <Setter Property="Foreground" Value="White" /> 
     <Setter Property="FontWeight" Value="Bold" /> 
     <Setter Property="FontSize" Value="13" /> 
     <Setter Property="FontFamily" Value="Arial" /> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Label"> 
      <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
       <Rectangle Margin="0 3 0 -3" Fill="LightGray" 
         RadiusX="11" RadiusY="11" Opacity="0.8"/> 
       <Border CornerRadius="11" 
         BorderBrush="DarkGray" 
         BorderThickness="1"> 
       <Border 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" CornerRadius="10" 
         Background="#FFC90000" 
         BorderBrush="White" 
         BorderThickness="2"> 
        <Grid> 
        <ContentPresenter 
          HorizontalAlignment="Center" VerticalAlignment="Center" 
          Content="{TemplateBinding Content}" Margin="5 1 6 1"/> 
        <Rectangle x:Name="TopShine" RadiusX="9" RadiusY="9" 
          VerticalAlignment="Stretch"> 
         <Rectangle.Fill> 
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity="0.6"> 
          <GradientStop Color="White" Offset="0.2" /> 
          <GradientStop Color="Transparent" Offset="0.7" /> 
         </LinearGradientBrush> 
         </Rectangle.Fill> 
        </Rectangle> 
        </Grid> 
       </Border> 
       </Border> 
      </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Page.Resources> 
    <Grid> 
    <UniformGrid> 
     <Label Style="{StaticResource CircularLabel}">4</Label> 
     <Label Style="{StaticResource CircularLabel}">100000</Label> 
     <Label Style="{StaticResource CircularLabel}">CLICK HERE</Label> 
    </UniformGrid> 
    </Grid> 
</Page> 
7

我最近有同样的要求,并迅速将此UserControl敲在一起。 它使用简短的动画将用户的注意力吸引到徽章上。

看看“大尼克的”博客上看到一些优雅的代码应用此用户控件到其他的UIElement作为装饰器(正是一个“标志”是!)(!感谢尼克) http://blog.bignickolson.com/2009/10/15/overlaying-controls-in-wpf-with-adorners/

alt text

<UserControl x:Class="BadgeControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      Opacity="0.8" 
       ClipToBounds="False" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <Style TargetType="Label" x:Key="BadgeLabel"> 
      <Setter Property="Foreground" Value="White" /> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Setter Property="FontSize" Value="12" /> 
      <Setter Property="Height" Value="22" /> 
      <Setter Property="MinWidth" Value="22" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Label"> 
         <Border Name="badgeOuterBorder" CornerRadius="10" BorderBrush="White" BorderThickness="2" Background="#C80103"> 
          <Border.RenderTransform> 
           <!-- 
           The TranslateTransform moves the badge so that when used as an Adorner, it bleeds over the upper left 
           edge of the adorned control. 
           The ScaleTransform ensures the badge is initially invisible on load , 
           but gives the storyboard the ability to 'animate' it into visibility (by manipulating the ScaleTransform). 
           --> 
           <TransformGroup> 
            <TranslateTransform X="-8" Y="-8"/> 
            <ScaleTransform ScaleX="0" ScaleY="0" /> 
           </TransformGroup> 
          </Border.RenderTransform> 
          <Border.BitmapEffect> 
           <!-- Give some depth to the badge with a drop-shadow --> 
           <DropShadowBitmapEffect Color="Black" Direction="270" ShadowDepth="3" Softness="0.2" Opacity="1"/> 
          </Border.BitmapEffect> 
          <Border CornerRadius="8" Padding="5 0 5 0"> 
           <Border.Background> 
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity="0.8"> 
             <GradientStop Color="White" Offset="0" /> 
             <GradientStop Color="Transparent" Offset="0.6" /> 
            </LinearGradientBrush> 
           </Border.Background> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}"></ContentPresenter> 
          </Border> 
         </Border> 
         <ControlTemplate.Triggers> 
          <EventTrigger RoutedEvent="Loaded"> 
           <EventTrigger.Actions> 
            <BeginStoryboard> 
             <!-- 
             The following storyboard animates the ScaleTransform in both the X and Y planes, so that the 
             badge appears to 'pop' into visibility. 
             The 1 second delay ensures that the parent control is fully visible before the animation begins, 
             otherwise, the animation may actually run before the form has rendered to the screen. 
             --> 
             <Storyboard> 
              <DoubleAnimation 
             Storyboard.TargetName="badgeOuterBorder" 
             Storyboard.TargetProperty="(Border.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" 
             From="0" 
             To="0.75" 
             BeginTime="0:0:1" 
             Duration="0:0:0.5"> 
               <DoubleAnimation.EasingFunction> 
                <BackEase Amplitude='1' EasingMode='EaseOut' /> 
               </DoubleAnimation.EasingFunction> 
              </DoubleAnimation> 
              <DoubleAnimation 
             Storyboard.TargetName="badgeOuterBorder" 
             Storyboard.TargetProperty="(Border.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)" 
             From="0" 
             To="0.75" 
             BeginTime="0:0:1" 
             Duration="0:0:0.5"> 
               <DoubleAnimation.EasingFunction> 
                <BackEase Amplitude='1' EasingMode='EaseOut' /> 
               </DoubleAnimation.EasingFunction> 
              </DoubleAnimation> 
             </Storyboard> 
            </BeginStoryboard> 
           </EventTrigger.Actions> 
          </EventTrigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

    </UserControl.Resources> 

    <Grid HorizontalAlignment="Left" VerticalAlignment="Top" ClipToBounds="False"> 
     <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Name="d" ClipToBounds="False"> 
      <Label Style="{StaticResource BadgeLabel}" Content="Badge Text" ToolTip="Badge Tooltip" ClipToBounds="False" /> 
     </Grid> 
    </Grid> 
</UserControl> 
+0

我如何在窗口中实现这个很酷的徽章? – IamStalker 2012-02-15 10:48:46

+1

你能更具体吗?然后我可以发布代码来完成它。 Thx – Adam 2012-02-20 21:05:35

+0

谢谢你,我已经找到了解决我的问题的方法。 – IamStalker 2012-02-21 06:06:03