亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于WPF實現(xiàn)路徑圖標(biāo)控件

 更新時間:2023年07月28日 09:09:17   作者:WPF開發(fā)者  
這篇文章主要介紹了如何利用WPF實現(xiàn)路徑圖標(biāo)控件,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下

WPF 實現(xiàn)路徑圖標(biāo)控件

框架使用.NET4

Visual Studio 2022;

實現(xiàn)方法

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

定義PathIcon類,它繼承自Control類,新增兩個依賴屬性

  • Kind屬性是一個枚舉類型的依賴屬性,用于指定圖標(biāo)的種類。
  • Data屬性是一個Geometry類型的依賴屬性,用于存儲圖標(biāo)的路徑數(shù)據(jù)。

OnKindChanged當(dāng)Kind屬性發(fā)生變化時會被調(diào)用。它首先獲取新值,并根據(jù)新值構(gòu)建資源名稱。然后,通過調(diào)用FindResource方法查找對應(yīng)的$"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控件,以實現(xiàn)路徑圖標(biāo)的縮放效果。

<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>

效果圖

到此這篇關(guān)于基于WPF實現(xiàn)路徑圖標(biāo)控件的文章就介紹到這了,更多相關(guān)WPF路徑圖標(biāo)控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#11新特性預(yù)覽及使用介紹

    C#11新特性預(yù)覽及使用介紹

    這篇文章主要為大家介紹了C#11新特性預(yù)覽及使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • c# 獲得當(dāng)前絕對路徑的方法(超簡單)

    c# 獲得當(dāng)前絕對路徑的方法(超簡單)

    下面小編就為大家分享一篇c# 獲得當(dāng)前絕對路徑的方法(超簡單),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • C#中的DataTable查詢實戰(zhàn)教程

    C#中的DataTable查詢實戰(zhàn)教程

    這篇文章主要介紹了C#中的DataTable查詢實戰(zhàn)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#線程間不能調(diào)用剪切板的解決方法

    C#線程間不能調(diào)用剪切板的解決方法

    這篇文章主要介紹了C#線程間不能調(diào)用剪切板的解決方法,需要的朋友可以參考下
    2014-07-07
  • .net中常用的正則表達(dá)式

    .net中常用的正則表達(dá)式

    這篇文章介紹了.net中常用的正則表達(dá)式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 深入理解C#指針之美

    深入理解C#指針之美

    在C#中,有時候希望通過指針來操作內(nèi)存,這樣可以提高效率。我們可以用unsafe關(guān)鍵字修飾含有指針操作的程序段,感興趣的小伙伴可以參考一下,希望可以幫到你
    2021-07-07
  • c# 基于Titanium爬取微信公眾號歷史文章列表

    c# 基于Titanium爬取微信公眾號歷史文章列表

    這篇文章主要介紹了c# 基于Titanium爬取微信公眾號歷史文章列表,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C# 中如何利用lambda實現(xiàn)委托事件的掛接

    C# 中如何利用lambda實現(xiàn)委托事件的掛接

    在寫一個小程序的時候,碰到了這樣的問題,需要用委托來掛接事件,但是又想在這事件中使用局部的變量,而委托一旦定義好后,掛接方就沒有辦法再添加額外的形參了。那有沒有什么辦法,可以實現(xiàn)呢
    2013-07-07
  • CefSharp如何進(jìn)行頁面的縮放(Ctrl+滾輪)

    CefSharp如何進(jìn)行頁面的縮放(Ctrl+滾輪)

    CefSharp簡單來說就是一款.Net編寫的瀏覽器包,本文主要介紹了CefSharp如何進(jìn)行頁面的縮放,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C# 標(biāo)準(zhǔn)事件流實例代碼

    C# 標(biāo)準(zhǔn)事件流實例代碼

    這篇文章主要介紹了C# 標(biāo)準(zhǔn)事件流的實例代碼,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論