WPF實現(xiàn)Badge標識的示例代碼
WPF 實現(xiàn) Badge 標識
框架使用.NET4 至 .NET6
Visual Studio 2022
新建 Badge.cs
繼承裝飾器 Adorner
增加依賴屬性
Text
用來展示文本FontSize
文本大小IsShow
為布爾值用來是否顯示Badge
IsShow = true
不設(shè)置其他屬性時候只顯示一個小紅點- 如果設(shè)置了
Text
則根據(jù)文本的寬度高度繪制小圓點的大小
默認將其繪制到控件右上角。
實現(xiàn)代碼
1)新建 Badge.cs
控件代碼如下:
using?System; using?System.Windows; using?System.Windows.Documents; using?System.Windows.Media; namespace?WPFDevelopers.Controls { ????public?class?Badge?:?Adorner ????{ ????????public?static?readonly?DependencyProperty?TextProperty?= ????????????DependencyProperty.Register("Text",?typeof(string),?typeof(Badge),?new?PropertyMetadata(string.Empty)); ????????public?static?readonly?DependencyProperty?FontSizeProperty?= ????????????DependencyProperty.Register("FontSize",?typeof(double),?typeof(Badge),?new?PropertyMetadata(10.0d)); ????????public?static?readonly?DependencyProperty?IsShowProperty?= ????????????DependencyProperty.RegisterAttached("IsShow",?typeof(bool),?typeof(Badge), ????????????????new?PropertyMetadata(false,?OnIsBadgeChanged)); ????????private?static?FrameworkElement?oldFrameworkElement; ????????private?readonly?double?_size; ????????private?readonly?string?_text; ????????public?Badge(UIElement?adornedElement,?string?text?=?null,?double?size?=?0) ????????????:?base(adornedElement) ????????{ ????????????_text?=?text; ????????????_size?=?size; ????????????ToolTip?=?text; ????????} ????????public?string?Text ????????{ ????????????get?=>?(string)?GetValue(TextProperty); ????????????set?=>?SetValue(TextProperty,?value); ????????} ????????public?double?FontSize ????????{ ????????????get?=>?(double)?GetValue(FontSizeProperty); ????????????set?=>?SetValue(FontSizeProperty,?value); ????????} ????????public?static?string?GetText(UIElement?element) ????????{ ????????????if?(element?==?null)?throw?new?ArgumentNullException("Text"); ????????????return?(string)?element.GetValue(TextProperty); ????????} ????????public?static?void?SetText(UIElement?element,?string?Text) ????????{ ????????????if?(element?==?null)?throw?new?ArgumentNullException("Text"); ????????????element.SetValue(TextProperty,?Text); ????????} ????????public?static?double?GetFontSize(UIElement?element) ????????{ ????????????if?(element?==?null)?throw?new?ArgumentNullException("FontSize"); ????????????return?(double)?element.GetValue(FontSizeProperty); ????????} ????????public?static?void?SetFontSize(UIElement?element,?string?Text) ????????{ ????????????if?(element?==?null)?throw?new?ArgumentNullException("FontSize"); ????????????element.SetValue(FontSizeProperty,?Text); ????????} ????????private?static?void?OnIsBadgeChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e) ????????{ ????????????if?(e.NewValue?is?bool?isBadge?&&?d?is?FrameworkElement?parent) ????????????{ ????????????????if?(isBadge) ????????????????{ ????????????????????if?(!parent.IsLoaded) ????????????????????????parent.Loaded?+=?Parent_Loaded; ????????????????????else ????????????????????????CreateBadge(parent); ????????????????} ????????????????else ????????????????{ ????????????????????parent.Loaded?-=?Parent_Loaded; ????????????????????CreateBadge(parent,?true); ????????????????} ????????????} ????????} ????????private?static?void?Parent_Loaded(object?sender,?RoutedEventArgs?e) ????????{ ????????????if?(sender?is?UIElement?element) ????????????????CreateBadge(element); ????????} ????????private?static?void?CreateBadge(UIElement?uIElement,?bool?isRemove?=?false) ????????{ ????????????var?layer?=?AdornerLayer.GetAdornerLayer(uIElement); ????????????if?(layer?==?null)?return; ????????????if?(isRemove?&&?uIElement?!=?null) ????????????{ ????????????????var?adorners?=?layer.GetAdorners(uIElement); ????????????????if?(adorners?!=?null) ????????????????????foreach?(var?item?in?adorners) ????????????????????????if?(item?is?Badge?container) ????????????????????????????layer.Remove(container); ????????????????return; ????????????} ????????????var?value?=?GetText(uIElement); ????????????var?size?=?GetFontSize(uIElement); ????????????var?badgeAdorner?=?new?Badge(uIElement,?value,?size); ????????????layer.Add(badgeAdorner); ????????} ????????public?static?bool?GetIsShow(DependencyObject?obj) ????????{ ????????????return?(bool)?obj.GetValue(IsShowProperty); ????????} ????????public?static?void?SetIsShow(DependencyObject?obj,?bool?value) ????????{ ????????????obj.SetValue(IsShowProperty,?value); ????????} ????????protected?override?void?OnRender(DrawingContext?drawingContext) ????????{ ????????????var?adornedElement?=?AdornedElement?as?FrameworkElement; ????????????var?margin?=?adornedElement.Margin; ????????????var?desiredWidth?=?adornedElement.DesiredSize.Width?-?margin.Left?-?margin.Right; ????????????var?brush?=?new?SolidColorBrush((Color)?Application.Current.TryFindResource("WD.DangerColor")); ????????????brush.Freeze(); ????????????var?radius?=?5.0; ????????????var?center?=?new?Point(desiredWidth,?0); ????????????FormattedText?formattedText?=?null; ????????????if?(!string.IsNullOrEmpty(_text)) ????????????????formattedText?=?DrawingContextHelper.GetFormattedText( ????????????????????_text, ????????????????????Brushes.White, ????????????????????FlowDirection.LeftToRight, ????????????????????_size); ????????????var?pen?=?new?Pen(Brushes.White,?.3); ????????????pen.Freeze(); ????????????drawingContext.PushTransform(new?MatrixTransform(Matrix.Identity)); ????????????if?(formattedText?!=?null) ????????????{ ????????????????var?height?=?formattedText.Height; ????????????????var?width?=?formattedText.Width?>?20???20?:?formattedText.Width; ????????????????var?isSingle?=?false; ????????????????if?(_text.Length?==?1) ????????????????{ ????????????????????var?max?=?formattedText.Width?>?formattedText.Height???formattedText.Width?:?formattedText.Height; ????????????????????height?=?max; ????????????????????width?=?max; ????????????????????isSingle?=?true; ????????????????} ????????????????var?startPoint?=?new?Point(0,?0); ????????????????var?endPoint?=?new?Point(0,?0); ????????????????if?(!isSingle) ????????????????{ ????????????????????startPoint?=?new?Point(center.X?-?width?/?1.4,?center.Y?-?height?/?1.8); ????????????????????endPoint?=?new?Point(center.X?+?width?/?1.4?+?6,?center.Y?+?height?/?1.8); ????????????????} ????????????????else ????????????????{ ????????????????????startPoint?=?new?Point(center.X?-?width?/?2,?center.Y?-?height?/?2); ????????????????????endPoint?=?new?Point(center.X?+?width?/?2,?center.Y?+?height?/?2); ????????????????} ????????????????var?rect?=?new?Rect(startPoint,?endPoint); ????????????????drawingContext.DrawRoundedRectangle(brush,?pen,?rect,?8,?8); ????????????????formattedText.MaxTextWidth?=?width?+?10; ????????????????var?centerRect?=?new?Point(rect.Left?+?rect.Width?/?2,?rect.Top?+?rect.Height?/?2); ????????????????var?textPosition?=?new?Point(centerRect.X?-?formattedText.Width?/?2, ????????????????????centerRect.Y?-?formattedText.Height?/?2); ????????????????drawingContext.DrawText(formattedText,?textPosition); ????????????} ????????????else ????????????{ ????????????????drawingContext.DrawEllipse(brush,?pen,?center,?radius,?radius); ????????????} ????????????drawingContext.Pop(); ????????????RenderOptions.SetEdgeMode(this,?EdgeMode.Unspecified); ????????} ????} }
2)新建 BadgeExample.xaml
使用 Badge
控件代碼如下:
<UserControl ????x:Class="WPFDevelopers.Samples.ExampleViews.BadgeExample" ????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ????xmlns:d="http://schemas.microsoft.com/expression/blend/2008" ????xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews" ????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ????xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers" ????d:DesignHeight="450" ????d:DesignWidth="800" ????mc:Ignorable="d"> ????????<Grid> ????????????<Grid.RowDefinitions> ????????????????<RowDefinition?Height="Auto"?/> ????????????????<RowDefinition?Height="Auto"?/> ????????????</Grid.RowDefinitions> ????????????<ToggleButton ????????????????x:Name="MyBadgeToggleButton" ????????????????Margin="10" ????????????????HorizontalAlignment="Center" ????????????????Content="顯示Badge"?/> ????????????<StackPanel ????????????????Grid.Row="1" ????????????????HorizontalAlignment="Center" ????????????????Orientation="Horizontal"> ????????????????<Button ????????????????????wd:Badge.IsShow="{Binding?ElementName=MyBadgeToggleButton,?Path=IsChecked}" ????????????????????wd:Badge.Text="new" ????????????????????Content="Default" ????????????????????Style="{DynamicResource?WD.PrimaryButton}"?/> ????????????????<Button ????????????????????Margin="40,0" ????????????????????wd:Badge.FontSize="12" ????????????????????wd:Badge.IsShow="{Binding?ElementName=MyBadgeToggleButton,?Path=IsChecked}" ????????????????????wd:Badge.Text="3" ????????????????????Content="Success" ????????????????????Style="{DynamicResource?WD.SuccessDefaultButton}"?/> ????????????????<Button ????????????????????wd:Badge.FontSize="12" ????????????????????wd:Badge.IsShow="{Binding?ElementName=MyBadgeToggleButton,?Path=IsChecked}" ????????????????????wd:Badge.Text="10+" ????????????????????Content="Warning" ????????????????????Style="{DynamicResource?WD.WarningDefaultButton}"?/> ????????????????<Button ????????????????????Margin="40,0" ????????????????????wd:Badge.IsShow="{Binding?ElementName=MyBadgeToggleButton,?Path=IsChecked}" ????????????????????wd:Badge.Text="NEW" ????????????????????Content="Danger" ????????????????????Style="{DynamicResource?WD.DangerDefaultButton}"?/> ????????????????<Rectangle ????????????????????Width="100" ????????????????????Height="100" ????????????????????wd:Badge.IsShow="{Binding?ElementName=MyBadgeToggleButton,?Path=IsChecked}" ????????????????????Fill="Aqua"?/> ????????????</StackPanel> ????????</Grid> </UserControl>
效果圖
以上就是WPF實現(xiàn)Badge標識的示例代碼的詳細內(nèi)容,更多關(guān)于WPF Badge標識的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c#?理解csredis庫實現(xiàn)分布式鎖的詳細流程
這篇文章主要介紹了c#?理解csredis實現(xiàn)分布式鎖,該庫本身已經(jīng)足夠完善,這里我畫蛇添足一下,為了方便自己的使用,本文通過實例代碼給大家詳細介紹,需要的朋友可以參考下2022-02-02C#利用ASP.NET?Core開發(fā)學(xué)生管理系統(tǒng)詳解
隨著技術(shù)的進步,跨平臺開發(fā)已經(jīng)成為了標配,在此大背景下,ASP.NET?Core也應(yīng)運而生。本文主要利用ASP.NET?Core開發(fā)一個學(xué)生管理系統(tǒng),感興趣的可以學(xué)習(xí)一下2022-01-01C#實現(xiàn)的4種常用數(shù)據(jù)校驗方法小結(jié)(CRC校驗,LRC校驗,BCC校驗,累加和校驗)
本文主要介紹了C#實現(xiàn)的4種常用數(shù)據(jù)校驗方法小結(jié)(CRC校驗,LRC校驗,BCC校驗,累加和校驗),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03C#將布爾類型轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將布爾類型轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#中字符串函數(shù)的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04unity實現(xiàn)貼圖矩陣運算(旋轉(zhuǎn)平移縮放)
這篇文章主要為大家詳細介紹了unity實現(xiàn)貼圖矩陣運算,旋轉(zhuǎn)平移縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07在C#中根據(jù)HardwareID獲取驅(qū)動程序信息的實現(xiàn)代碼
這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅(qū)動程序信息的實現(xiàn)代碼,需要的朋友可以參考下2016-12-12unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04