基于WPF实现路径图标控件

 更新时间:2023年07月28日 09:09:17   作者:WPF开发者  
这篇文章主要介绍了如何利用WPF实现路径图标控件,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的小伙伴可以参考一下

WPF 实现路径图标控件

框架使用.NET4

Visual Studio 2022;

实现方法

1)新增 PathIcon.cs 代码如下:

定义PathIcon类,它继承自Control类,新增两个依赖属性

  • Kind属性是一个枚举类型的依赖属性,用于指定图标的种类。
  • Data属性是一个Geometry类型的依赖属性,用于存储图标的路径数据。

OnKindChangedKind属性发生变化时会被调用。它首先获取新值,并根据新值构建资源名称。然后,通过调用FindResource方法查找对应的$"WD.{kind}Geometry"资源,并将其赋值给Data属性。

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFDevelopers.Controls
{
    public class PathIcon : Control
    {
        public static readonly DependencyProperty KindProperty =
            DependencyProperty.Register(nameof(Kind), typeof(string), typeof(PathIcon),
                new PropertyMetadata(string.Empty, OnKindChanged));
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register(nameof(Data), typeof(Geometry), typeof(PathIcon));
        public PackIconKind Kind
        {
            get { return (PackIconKind)GetValue(KindProperty); }
            set { SetValue(KindProperty, value); }
        }
        public Geometry Data
        {
            get { return (Geometry)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }
        private static void OnKindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var pathIcon = (PathIcon)d;
            var kind = (string)e.NewValue;
            if (!string.IsNullOrWhiteSpace(kind))
            {
                kind = $"WD.{kind}Geometry";
                pathIcon.Data = (Geometry)pathIcon.FindResource(kind);
            }
            else
                pathIcon.Data = null;
        }
        static PathIcon()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PathIcon), new FrameworkPropertyMetadata(typeof(PathIcon)));
        }
    }
}

2)新增 PathIcon.xaml 代码如下:

使用Viewbox控件包裹Path控件,以实现路径图标的缩放效果。

<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">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Basic/ControlBasic.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style
        x:Key="WD.PathIcon"
        BasedOn="{StaticResource WD.ControlBasicStyle}"
        TargetType="{x:Type controls:PathIcon}">
        <Setter Property="Padding" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Height" Value="16" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Setter Property="Foreground">
            <Setter.Value>
                <Binding Path="Foreground" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Control}}" />
            </Setter.Value>
        </Setter>
        <Setter Property="Width" Value="16" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:PathIcon}">
                    <Viewbox
                        Margin="{TemplateBinding Padding}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        UseLayoutRounding="True">
                        <Path
                            x:Name="PART_Path"
                            Data="{TemplateBinding Data}"
                            Fill="{TemplateBinding Foreground}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            Stretch="Uniform"
                            UseLayoutRounding="False" />
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="WD.MiniPathIcon"
        BasedOn="{StaticResource WD.PathIcon}"
        TargetType="{x:Type controls:PathIcon}">
        <Setter Property="Height" Value="10" />
        <Setter Property="Width" Value="7" />
    </Style>
    <Style BasedOn="{StaticResource WD.PathIcon}" TargetType="{x:Type controls:PathIcon}" />
</ResourceDictionary>

3)新增示例 PathIconExample.xaml 代码如下:

<UniformGrid
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Columns="6"
            Rows="2">
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.PrimaryButton}">
                <wd:PathIcon Data="M682 256h256v256l-98-98-268 268-170-170-256 256-60-60 316-316 170 170 208-208z" />
            </Button>
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.DangerPrimaryButton}">
                <wd:PathIcon Kind="Arrow" />
            </Button>
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.DangerDefaultButton}">
                <wd:PathIcon Kind="SortArrow" />
            </Button>
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.WarningDefaultButton}">
                <wd:PathIcon Kind="SmileyOutline" />
            </Button>
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.DefaultButton}">
                <wd:PathIcon Kind="Replace" />
            </Button>
            <Button
                Margin="4"
                wd:Badge.HorizontalOffset="17"
                wd:Badge.IsShow="True"
                wd:Badge.VerticalOffset="8"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.SuccessDefaultButton}">
                <wd:PathIcon Kind="Home" />
            </Button>
            <Button
                Margin="4"
                wd:ElementHelper.IsRound="True"
                Style="{StaticResource WD.NormalButton}">
                <wd:PathIcon PathData="M682 256h256v256l-98-98-268 268-170-170-256 256-60-60 316-316 170 170 208-208z" />
            </Button>
            <Button Margin="4" Style="{StaticResource WD.SuccessPrimaryButton}">
                <wd:PathIcon Kind="Arrow" />
            </Button>
            <Button Margin="4" Style="{StaticResource WD.DangerPrimaryButton}">
                <wd:PathIcon Kind="SortArrow" />
            </Button>
            <Button
                Margin="4"
                wd:Badge.IsShow="True"
                Style="{StaticResource WD.WarningPrimaryButton}">
                <wd:PathIcon
                    Width="20"
                    Height="20"
                    Kind="SmileyOutline" />
            </Button>
            <Button Margin="4" Style="{StaticResource WD.PrimaryButton}">
                <wd:PathIcon Kind="Replace" />
            </Button>
            <Button Margin="4" Style="{StaticResource WD.SuccessPrimaryButton}">
                <StackPanel Orientation="Horizontal">
                    <wd:PathIcon Kind="Home" />
                    <TextBlock Margin="4,0" Text="Home" />
                </StackPanel>
            </Button>
        </UniformGrid>

效果图

到此这篇关于基于WPF实现路径图标控件的文章就介绍到这了,更多相关WPF路径图标控件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# 多态性的深入理解

    C# 多态性的深入理解

    本篇文章是对C#中的多态性进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#中的两种debug方法介绍

    C#中的两种debug方法介绍

    这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹、利用宏定义两种方法,需要的朋友可以参考下
    2015-02-02
  • C#中字符串优化String.Intern、IsInterned详解

    C#中字符串优化String.Intern、IsInterned详解

    这篇文章主要给大家介绍了关于C#中字符串优化String.Intern、IsInterned的相关资料,文中通过示例代码介绍的,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-12-12
  • C#实现Modbus通信功能的示例详解

    C#实现Modbus通信功能的示例详解

    Modbus作为一种开放且广泛采用的通信协议,在实现设备间数据交换方面发挥着至关重要的作用,它不仅支持多种物理层接口(如RS-232, RS-485, 以及以太网),还因其简单易用的特点而被大家所青睐,本文通过实际示例介绍如何在C#项目中轻松实现Modbus通信功能
    2024-11-11
  • C#垃圾回收机制的详细介绍

    C#垃圾回收机制的详细介绍

    这篇文章详细介绍了C#垃圾回收机制,有需要的朋友可以参考一下
    2013-09-09
  • WPF使用Accord实现屏幕录制功能

    WPF使用Accord实现屏幕录制功能

    这篇文章主要为大家详细介绍了WPF如何使用Accord实现屏幕录制,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2024-03-03
  • C# wx获取token的基本方法

    C# wx获取token的基本方法

    这篇文章主要为大家详细介绍了C# wx获取token的基本方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • 使用C#实现对任意区域任意大小的截图

    使用C#实现对任意区域任意大小的截图

    这篇文章主要为大家详细介绍了如何使用C#实现简单的截图功能,可以对任意区域任意大小的截图,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • C#连接SQLite数据库并实现基本操作

    C#连接SQLite数据库并实现基本操作

    本文介绍了SQLite,一个轻量级的跨平台数据库管理系统,以及如何在C#中使用System.Data.SQLite库进行操作,包括创建、修改和查询数据库,以及使用SQLiteHelper类简化SQL使用,此外,还提到了DB文件查看工具SQLiteSpy的应用,需要的朋友可以参考下
    2024-12-12
  • C#过滤sql特殊字符串的方法

    C#过滤sql特殊字符串的方法

    这篇文章介绍了C#过滤sql特殊字符串的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07

最新评论