add Badge

This commit is contained in:
NaBian committed 2019-03-07 00:12:44 +08:00
1 parent 785b2f25f5
commit 411541d578
10 files changed
+70 -29

No files matched your search

@@ -1,5 +1,4 @@
using System;
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using HandyControl.Data;
@@ -25,10 +24,6 @@ namespace HandyControl.Controls
{
if ((bool)e.NewValue)
{
border.SetBinding(FrameworkElement.MinWidthProperty,
new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = border });
border.SetBinding(FrameworkElement.MinHeightProperty,
new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = border });
var binding = new MultiBinding
{
Converter = new BorderCircularConverter()
@@ -4,6 +4,9 @@ using HandyControl.Data;
namespace HandyControl.Controls
{
/// <summary>
/// 标记
/// </summary>
public class Badge : ContentControl
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
@@ -22,7 +25,7 @@ namespace HandyControl.Controls
{
var ctl = (Badge) d;
var v = (int) e.NewValue;
ctl.Text = v.ToString();
ctl.Text = v <= ctl.Maximum ? v.ToString() : $"{ctl.Maximum}+";
}
public int Value
@@ -41,12 +44,21 @@ namespace HandyControl.Controls
}
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
"Maximum", typeof(int), typeof(Badge), new PropertyMetadata(ValueBoxes.Int99Box));
"Maximum", typeof(int), typeof(Badge), new PropertyMetadata(ValueBoxes.Int99Box, OnValueChanged));
public int Maximum
{
get => (int) GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}
public static readonly DependencyProperty BadgeMarginProperty = DependencyProperty.Register(
"BadgeMargin", typeof(Thickness), typeof(Badge), new PropertyMetadata(default(Thickness)));
public Thickness BadgeMargin
{
get => (Thickness) GetValue(BadgeMarginProperty);
set => SetValue(BadgeMarginProperty, value);
}
}
}
@@ -4,6 +4,12 @@ using System.Windows.Controls;
namespace HandyControl.Controls
{
/// <summary>
/// 用以代替Grid
/// </summary>
/// <remarks>
/// 当不需要Grid的行、列分隔等功能时建议用此轻量级类代替
/// </remarks>
public class SimplePanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
+1 -1
View File
@@ -109,7 +109,7 @@
<Compile Include="Controls\Attach\TitleElement.cs" />
<Compile Include="Controls\Button\ContextMenuButton.cs" />
<Compile Include="Controls\Button\ContextMenuToggleButton.cs" />
<Compile Include="Controls\Badge\Badge.cs" />
<Compile Include="Controls\Other\Badge.cs" />
<Compile Include="Controls\Other\Carousel.cs" />
<Compile Include="Controls\Cover\CoverView\CoverView.cs" />
<Compile Include="Controls\Other\ChatBubble.cs" />
+5 -5
View File
@@ -10,23 +10,23 @@
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
</Style>
<Style x:Key="BadgeChromePrimary" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Style x:Key="BadgePrimary" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
</Style>
<Style x:Key="BadgeChromeSuccess" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Style x:Key="BadgeSuccess" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource SuccessBrush}"/>
</Style>
<Style x:Key="BadgeChromeInfo" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Style x:Key="BadgeInfo" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource InfoBrush}"/>
</Style>
<Style x:Key="BadgeChromeWarning" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Style x:Key="BadgeWarning" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource WarningBrush}"/>
</Style>
<Style x:Key="BadgeChromeDanger" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Style x:Key="BadgeDanger" BasedOn="{StaticResource BadgeBaseStyle}" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource DangerBrush}"/>
</Style>
@@ -6,24 +6,39 @@
<ResourceDictionary Source="../Border.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ControlTemplate x:Key="BadgeTextTemplate" TargetType="controls:Badge">
<controls:SimplePanel>
<ContentPresenter/>
<Border Style="{StaticResource BorderCircular}" Margin="{TemplateBinding BadgeMargin}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter ContentSource="Text" Margin="{TemplateBinding Padding}"/>
</Border>
</controls:SimplePanel>
</ControlTemplate>
<ControlTemplate x:Key="BadgeDotTemplate" TargetType="controls:Badge">
<controls:SimplePanel>
<ContentPresenter/>
<Border Margin="{TemplateBinding BadgeMargin}" Width="10" Height="10" CornerRadius="5" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
</controls:SimplePanel>
</ControlTemplate>
<Style x:Key="BadgeBaseStyle" TargetType="controls:Badge">
<Setter Property="Background" Value="{DynamicResource SecondaryRegionBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource TextIconBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource RegionBrush}"/>
<Setter Property="Padding" Value="6,2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Badge">
<controls:SimplePanel>
<ContentPresenter/>
<Border Margin="0,-10,-20,0" HorizontalAlignment="Right" VerticalAlignment="Top" Style="{StaticResource BorderCircular}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter ContentSource="Text" Margin="{TemplateBinding Padding}"/>
</Border>
</controls:SimplePanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template" Value="{StaticResource BadgeTextTemplate}"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="BadgeMargin" Value="0,-10,-10,0"/>
<Style.Triggers>
<Trigger Property="IsDot" Value="True">
<Setter Property="Template" Value="{StaticResource BadgeDotTemplate}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="BadgeMargin" Value="0,-5,-5,0"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>