最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實現(xiàn)圓形進度條的示例代碼

 更新時間:2023年01月04日 09:35:12   作者:驚鏵  
這篇文章主要為大家詳細介紹了WPF如何實現(xiàn)圓形的進度條,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下

WPF 實現(xiàn)圓形進度條

  • 框架使用.NET40;
  • Visual Studio 2019;
  • CircularProgressBar 繼承 ProgressBar,在 XAML 中創(chuàng)建兩個 Path 的 Data 設(shè)置 ArcSegment 修改第二個控件的 Point ,設(shè)置 StartPoint = new Point(Size.Width, 0) 設(shè)置起點。
  • 創(chuàng)建依賴屬性 Angle 作為修改 ArcSegment 的 Point 作為進度條的圓的閉合。
  • 當進度條 ValueChanged 時創(chuàng)建 DoubleAnimation 動畫,修改 Angle 。

示例代碼

1) CircularProgressBar.xaml 代碼如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:controls="clr-namespace:WPFDevelopers.Controls"
                    xmlns:convert="clr-namespace:WPFDevelopers.Converts">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <convert:AngleToPointConverter x:Key="prConverter"/>
    <convert:AngleToIsLargeConverter x:Key="isLargeConverter"/>
    <Style TargetType="{x:Type controls:CircularProgressBar}" BasedOn="{StaticResource ControlBasicStyle}">
        <Setter Property="Maximum" Value="100"/>
        <Setter Property="StrokeThickness" Value="10"/>
        <Setter Property="Foreground" Value="{DynamicResource InfoSolidColorBrush}"/>
        <Setter Property="Background" Value="{DynamicResource PrimaryNormalSolidColorBrush}"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <!--<Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="100"/>-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:CircularProgressBar}">
                    <controls:SmallPanel Width="{Binding ElementName=PART_Path,Path=ActualWidth}" 
                            Height="{Binding ElementName=PART_Path,Path=ActualHeight}"
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <Canvas>
                            <Path Stroke="{TemplateBinding BorderBrush}"
                              StrokeThickness="{TemplateBinding BrushStrokeThickness}"
                                  x:Name="PART_Path">
                                <Path.Data>
                                    <PathGeometry>
                                        <PathFigure x:Name="PART_PathFigure">
                                            <ArcSegment SweepDirection="Clockwise"
                                                    IsLargeArc="True"
                                                    x:Name="PART_ArcSegment">
                                            </ArcSegment>
                                        </PathFigure>
                                    </PathGeometry>
                                </Path.Data>
                            </Path>
                            <Path Stroke="{TemplateBinding Background}" 
                              StrokeThickness="{TemplateBinding StrokeThickness}">
                                <Path.Data>
                                    <PathGeometry>
                                        <PathFigure x:Name="PART_PathFigureAngle">
                                            <ArcSegment SweepDirection="Clockwise"
                                                    IsLargeArc="{Binding Path=Angle, Converter={StaticResource isLargeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=ProgressBar}}"
                                                    x:Name="PART_ArcSegmentAngle">
                                                <ArcSegment.Point>
                                                    <MultiBinding Converter="{StaticResource prConverter}">
                                                        <Binding Path="Angle" RelativeSource="{RelativeSource FindAncestor, AncestorType=ProgressBar}"/>
                                                        <Binding Path="Size" RelativeSource="{RelativeSource FindAncestor, AncestorType=ProgressBar}"/>
                                                    </MultiBinding>
                                                </ArcSegment.Point>
                                            </ArcSegment>
                                        </PathFigure>
                                    </PathGeometry>
                                </Path.Data>
                            </Path>
                        </Canvas>
                        <TextBlock Foreground="{TemplateBinding Foreground}" 
                                   Text="{Binding Path=Value, StringFormat={}{0}%, 
                                RelativeSource={RelativeSource TemplatedParent}}"
                                   FontSize="{TemplateBinding FontSize}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center"
                                   x:Name="PART_TextBlock"/>
                    </controls:SmallPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

2) CircularProgressBar.xaml.cs 代碼如下:

using?System;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Media;
using?System.Windows.Media.Animation;

namespace?WPFDevelopers.Controls
{
????[TemplatePart(Name?=?ArcSegmentTemplateName,?Type?=?typeof(ArcSegment))]
????[TemplatePart(Name?=?ArcSegmentAngleTemplateName,?Type?=?typeof(ArcSegment))]
????[TemplatePart(Name?=?PathFigureTemplateName,?Type?=?typeof(PathFigure))]
????[TemplatePart(Name?=?PathFigureAngleTemplateName,?Type?=?typeof(PathFigure))]
????[TemplatePart(Name?=?TextBlockTemplateName,?Type?=?typeof(TextBlock))]
????public?class?CircularProgressBar?:?ProgressBar
????{
????????private?const?string?ArcSegmentTemplateName?=?"PART_ArcSegment";
????????private?const?string?ArcSegmentAngleTemplateName?=?"PART_ArcSegmentAngle";
????????private?const?string?PathFigureTemplateName?=?"PART_PathFigure";
????????private?const?string?PathFigureAngleTemplateName?=?"PART_PathFigureAngle";
????????private?const?string?TextBlockTemplateName?=?"PART_TextBlock";
????????private?ArcSegment?_arcSegment,?_arcSegmentAngle;
????????private?PathFigure?_pathFigure,?_pathFigureAngle;
????????private?TextBlock?_textBlock;


????????public?static?readonly?DependencyProperty?SizeProperty?=
???????????DependencyProperty.Register("Size",?typeof(Size),?typeof(CircularProgressBar),
???????????????new?PropertyMetadata(new?Size(50,50)));
????????public?static?readonly?DependencyProperty?AngleProperty?=
????????????DependencyProperty.Register("Angle",?typeof(double),?typeof(CircularProgressBar),
????????????????new?PropertyMetadata(0.0));

????????public?static?readonly?DependencyProperty?StrokeThicknessProperty?=
????????????DependencyProperty.Register("StrokeThickness",?typeof(double),?typeof(CircularProgressBar),
????????????????new?PropertyMetadata(10.0));

????????public?static?readonly?DependencyProperty?BrushStrokeThicknessProperty?=
????????????DependencyProperty.Register("BrushStrokeThickness",?typeof(double),?typeof(CircularProgressBar),
????????????????new?PropertyMetadata(1.0));

????????public?CircularProgressBar()
????????{
????????????ValueChanged?+=?CircularProgressBar_ValueChanged;
????????}
????????public?override?void?OnApplyTemplate()
????????{
????????????base.OnApplyTemplate();
???????????
????????????if?(Size.Width?!=?Size.Height)
????????????{
????????????????var?max?=?Math.Max(Size.Width,?Size.Height);
????????????????Size?=?new?Size(max,?max);
????????????}
???????????
????????????_pathFigure?=?GetTemplateChild(PathFigureTemplateName)?as?PathFigure;
????????????_pathFigureAngle?=?GetTemplateChild(PathFigureAngleTemplateName)?as?PathFigure;
????????????_pathFigure.StartPoint?=?new?Point(Size.Width,?0);
????????????_pathFigureAngle.StartPoint?=?new?Point(Size.Width,?0);
????????????_arcSegment?=?GetTemplateChild(ArcSegmentTemplateName)?as?ArcSegment;
????????????_arcSegment.Size?=?Size;
????????????_arcSegment.Point?=?new?Point(Size.Width?-?0.000872664626,?7.61543361704753E-09);
????????????_arcSegmentAngle?=?GetTemplateChild(ArcSegmentAngleTemplateName)?as?ArcSegment;
????????????_arcSegmentAngle.Size?=?Size;
????????????_textBlock?=?GetTemplateChild(TextBlockTemplateName)?as?TextBlock;
????????????if?(Size.Width?<?15)
????????????{
????????????????FontSize?=?8;
????????????}
????????}
????????
????????public?Size?Size
????????{
????????????get?=>?(Size)GetValue(SizeProperty);
????????????set?=>?SetValue(SizeProperty,?value);
????????}

????????public?double?Angle
????????{
????????????get?=>?(double)GetValue(AngleProperty);
????????????set?=>?SetValue(AngleProperty,?value);
????????}

????????public?double?StrokeThickness
????????{
????????????get?=>?(double)GetValue(StrokeThicknessProperty);
????????????set?=>?SetValue(StrokeThicknessProperty,?value);
????????}

????????public?double?BrushStrokeThickness
????????{
????????????get?=>?(double)GetValue(BrushStrokeThicknessProperty);
????????????set?=>?SetValue(BrushStrokeThicknessProperty,?value);
????????}

????????private?void?CircularProgressBar_ValueChanged(object?sender,?RoutedPropertyChangedEventArgs<double>?e)
????????{
????????????var?bar?=?sender?as?CircularProgressBar;
????????????var?currentAngle?=?bar.Angle;
????????????var?targetAngle?=?e.NewValue?/?bar.Maximum?*?359.999;
????????????var?anim?=?new?DoubleAnimation(currentAngle,?targetAngle,?TimeSpan.FromMilliseconds(500));
????????????bar.BeginAnimation(AngleProperty,?anim,?HandoffBehavior.SnapshotAndReplace);
????????}
????}
}

3) AngleToPointConverter.cs 代碼如下:

using?System;
using?System.Globalization;
using?System.Windows;
using?System.Windows.Data;

namespace?WPFDevelopers.Converts
{
????internal?class?AngleToPointConverter?:?IMultiValueConverter
????{
????????public?object?Convert(object[]?values,?Type?targetType,?object?parameter,?CultureInfo?culture)
????????{
????????????var?angle?=?(double)values[0];
????????????var?size?=?(Size)values[1];
????????????var?radius?=?(double)size.Height;
????????????var?piang?=?angle?*?Math.PI?/?180;

????????????var?px?=?Math.Sin(piang)?*?radius?+?radius;
????????????var?py?=?-Math.Cos(piang)?*?radius?+?radius;
????????????return?new?Point(px,?py);
????????}
????????public?object[]?ConvertBack(object?value,?Type[]?targetTypes,?object?parameter,?CultureInfo?culture)
????????{
????????????throw?new?NotImplementedException();
????????}
????}
}

4) AngleToIsLargeConverter.cs 代碼如下:

using?System;
using?System.Globalization;
using?System.Windows.Data;

namespace?WPFDevelopers.Converts
{
????internal?class?AngleToIsLargeConverter?:?IValueConverter
????{
????????public?object?Convert(object?value,?Type?targetType,?object?parameter,?CultureInfo?culture)
????????{
????????????var?angle?=?(double)value;
????????????return?angle?>?180;
????????}

????????public?object?ConvertBack(object?value,?Type?targetTypes,?object?parameter,?CultureInfo?culture)
????????{
????????????throw?new?NotImplementedException();
????????}
????}
}

5) CircularMenuExample.xaml 代碼如下:

<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.CircularMenuExample"
?????????????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"?
?????????????xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
?????????????xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"
?????????????xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
?????????????mc:Ignorable="d"?
?????????????d:DesignHeight="450"?d:DesignWidth="800">
????<controls:CodeViewer>
????????<StackPanel?Background="Black">
????????<TextBlock?Text="微信公眾號:WPFDevelopers"?FontSize="40"
???????????????????????????Foreground="#A9CC32"?FontWeight="Bold"
???????????????????????????Margin="50,10,0,20"/>
????????<wpfdev:CircularMenu?ItemsSource="{Binding?MenuArray,RelativeSource={RelativeSource?AncestorType=local:CircularMenuExample}}"?
?????????????????????????????SelectionChanged="CircularMenu_SelectionChanged"/>
????</StackPanel>
????????<controls:CodeViewer.SourceCodes>
????????????<controls:SourceCodeModel?
????????????????CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CircularMenuExample.xaml"?
????????????????CodeType="Xaml"/>
????????????<controls:SourceCodeModel?
????????????????CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CircularMenuExample.xaml.cs"?
????????????????CodeType="CSharp"/>
????????</controls:CodeViewer.SourceCodes>
????</controls:CodeViewer>
</UserControl>

效果圖

到此這篇關(guān)于WPF實現(xiàn)圓形進度條的示例代碼的文章就介紹到這了,更多相關(guān)WPF進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#操作SQLite方法實例詳解

    C#操作SQLite方法實例詳解

    這篇文章主要介紹了C#操作SQLite方法,以實例形式詳細分析了C#操作SQLite的連接、查詢、插入、修改等相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • Unity3D實現(xiàn)列表分頁效果

    Unity3D實現(xiàn)列表分頁效果

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)列表分頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#開發(fā)Android百度地圖手機應(yīng)用程序(多地圖展示)

    C#開發(fā)Android百度地圖手機應(yīng)用程序(多地圖展示)

    這篇文章主要介紹了C#開發(fā)Android百度地圖手機應(yīng)用程序(多地圖展示)的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • C#中的Action、Func和Predicate如何使用

    C#中的Action、Func和Predicate如何使用

    這篇文章主要給大家介紹了關(guān)于C#中Action、Func和Predicate如何使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 如何使用C#代碼創(chuàng)建快捷方式文件詳解

    如何使用C#代碼創(chuàng)建快捷方式文件詳解

    在Windows中創(chuàng)建快捷方式很簡單,如果想用C#代碼的方式創(chuàng)建,就沒有那么方便了,因為.NET框架沒有提供直接創(chuàng)建快捷方式的方法。這篇文章主要給大家介紹了關(guān)于如何使用C#代碼創(chuàng)建快捷方式文件的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • C#深度優(yōu)先遍歷實現(xiàn)全排列

    C#深度優(yōu)先遍歷實現(xiàn)全排列

    這篇文章主要介紹了C#深度優(yōu)先遍歷實現(xiàn)全排列,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • C#逐行讀取文件的方法

    C#逐行讀取文件的方法

    這篇文章主要介紹了C#逐行讀取文件的方法,針對較大文件的讀取非常實用,需要的朋友可以參考下
    2015-07-07
  • C#進行文件讀寫、創(chuàng)建、復(fù)制、移動、刪除的方法

    C#進行文件讀寫、創(chuàng)建、復(fù)制、移動、刪除的方法

    這篇文章主要介紹了C#進行文件讀寫、創(chuàng)建、復(fù)制、移動、刪除的方法,涉及C#針對文件操作的常用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • C#實現(xiàn)不同窗體之間傳遞參數(shù)

    C#實現(xiàn)不同窗體之間傳遞參數(shù)

    這篇文章主要介紹了C#實現(xiàn)不同窗體之間傳遞參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 仿orm自動生成分頁SQL分享

    仿orm自動生成分頁SQL分享

    平時接觸的數(shù)據(jù)庫有sql2000-2008,Oracle,SQLite 。 分頁邏輯,Oracle和SQLite相對好寫,就SQL事多,Sql2000下只能用top,排序2次,而Sql2005+就可以使用ROW_NUMBER()分析函數(shù)了,據(jù)說Sql2012對分頁又有了改進
    2014-01-01

最新評論

永吉县| 阜新市| 江华| 济南市| 德阳市| 兴业县| 洮南市| 常州市| 新干县| 县级市| 伊宁市| 阿城市| 锡林郭勒盟| 孙吴县| 鄯善县| 黄冈市| 清丰县| 江山市| 响水县| 六枝特区| 堆龙德庆县| 永兴县| 于都县| 光泽县| 皮山县| 吉安市| 府谷县| 万盛区| 凤山市| 米易县| 大港区| 池州市| 隆回县| 双辽市| 河南省| 宁武县| 齐齐哈尔市| 吴川市| 和林格尔县| 吉安县| 株洲县|