WPF依賴(lài)屬性用法詳解
一、什么是依賴(lài)屬性
依賴(lài)屬性就是一種自己可以沒(méi)有值,并且可以通過(guò)綁定從其他數(shù)據(jù)源獲取值。依賴(lài)屬性可支持WPF中的樣式設(shè)置、數(shù)據(jù)綁定、繼承、動(dòng)畫(huà)及默認(rèn)值。
將所有的屬性都設(shè)置為依賴(lài)屬性并不總是正確的解決方案,具體取決于其應(yīng)用場(chǎng)景。有時(shí),使用私有字段實(shí)現(xiàn)屬性的典型方法便能滿(mǎn)足要求。MSDN中給出了下面幾種應(yīng)用依賴(lài)屬性的場(chǎng)景:
- 1. 希望可在樣式中設(shè)置屬性。
- 2. 希望屬性支持?jǐn)?shù)據(jù)綁定。
- 3. 希望可使用動(dòng)態(tài)資源引用設(shè)置屬性。
- 4. 希望從元素樹(shù)中的父元素自動(dòng)繼承屬性值。
- 5. 希望屬性可進(jìn)行動(dòng)畫(huà)處理。
- 6. 希望屬性系統(tǒng)在屬性系統(tǒng)、環(huán)境或用戶(hù)執(zhí)行的操作或者讀取并使用樣式更改了屬性以前的值時(shí)報(bào)告。
- 7. 希望使用已建立的、WPF 進(jìn)程也使用的元數(shù)據(jù)約定,例如報(bào)告更改屬性值時(shí)是否要求布局系統(tǒng)重新編寫(xiě)元素的可視化對(duì)象。
二、依賴(lài)屬性的特點(diǎn)
1、屬性變更通知
無(wú)論什么時(shí)候,只要依賴(lài)屬性的值發(fā)生改變,wpf就會(huì)自動(dòng)根據(jù)屬性的元數(shù)據(jù)觸發(fā)一系列的動(dòng)作,這些動(dòng)作可以重新呈現(xiàn)UI元素,也可以更新當(dāng)前的布局,刷新數(shù)據(jù)綁定等等,這種變更的通知最有趣的特點(diǎn)之一就是屬性觸發(fā)器,它可以在屬性值改變的時(shí)候,執(zhí)行一系列自定義的動(dòng)作,而不需要更改任何其他的代碼來(lái)實(shí)現(xiàn)。通過(guò)下面的示例來(lái)演示屬性變更通知
示例:當(dāng)鼠標(biāo)移動(dòng)到Button按鈕上面時(shí),文字的前景色變?yōu)榧t色,離開(kāi)時(shí)變?yōu)槟J(rèn)顏色黑色,采用傳統(tǒng)方式和依賴(lài)屬性?xún)煞N方式實(shí)現(xiàn):
(1)、使用傳統(tǒng)方式實(shí)現(xiàn),在Button按鈕上定義MouseEnter和MouseLeave兩個(gè)事件,分別處理鼠標(biāo)移動(dòng)到按鈕上面和離開(kāi),XAML界面代碼:
<Window x:Class="WpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Grid面板" Height="237" Width="525" WindowStartupLocation="CenterScreen"> <Grid > <Button Height="30" Width="200" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" >鼠標(biāo)移動(dòng)到上面,前景色變?yōu)榧t色</Button> </Grid> </Window>
C#后臺(tái)代碼實(shí)現(xiàn):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfDemo { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 鼠標(biāo)移動(dòng)到按鈕上面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_MouseEnter(object sender, MouseEventArgs e) { Button btn = sender as Button; if (btn != null) { btn.Foreground = Brushes.Red; } } /// <summary> /// 鼠標(biāo)離開(kāi)按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_MouseLeave(object sender, MouseEventArgs e) { Button btn = sender as Button; if (btn != null) { btn.Foreground = Brushes.Black; } } } }
(2)使用依賴(lài)屬性實(shí)現(xiàn),XAML界面代碼:
<Window x:Class="WpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Grid面板" Height="237" Width="525" WindowStartupLocation="CenterScreen"> <Grid > <Button Height="30" Width="200">鼠標(biāo)移動(dòng)到上面,前景色變?yōu)榧t色 <Button.Style> <Style TargetType="Button"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red"></Setter> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> </Grid> </Window>
使用上面的兩種方式都可以實(shí)現(xiàn)Button按鈕的前景色改變,效果如下:
在判斷屬性IsMouseOver的值為false的時(shí)候,自動(dòng)將Foreground的值改為之前的值,因此就不需要寫(xiě)IsMouseOver的值為false的時(shí)候,將Foreground的值改為Black。
2、屬性值繼承
是指屬性值自頂向下沿著元素樹(shù)進(jìn)行傳遞。
<Window x:Class="WpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="依賴(lài)屬性" Height="237" Width="525" FontSize="18" WindowStartupLocation="CenterScreen"> <Grid > <StackPanel> <TextBlock>我使用的是繼承的fontsize</TextBlock> <TextBlock FontSize="11">我使用的是自己的fontsize</TextBlock> </StackPanel> </Grid> </Window>
界面運(yùn)行效果:
3、節(jié)省內(nèi)存空間
依賴(lài)屬性和CLR屬性在內(nèi)存的使用上是截然不同的,每個(gè)CLR屬性都包含一個(gè)非static的字段,因此當(dāng)我們實(shí)例化一個(gè)類(lèi)型的時(shí)候,就會(huì)創(chuàng)建該類(lèi)型所擁有的所有CLR屬性,也就是說(shuō)一個(gè)對(duì)象所占用的內(nèi)存在調(diào)用new操作進(jìn)行實(shí)例化的時(shí)候就已經(jīng)決定了、而wpf允許對(duì)象在創(chuàng)建的時(shí)候并不包含用于存儲(chǔ)數(shù)據(jù)的空間,只保留在需要用到數(shù)據(jù)的時(shí)候能夠獲得該默認(rèn)值,即用其他對(duì)象數(shù)據(jù)或者實(shí)時(shí)分配空間的能力。
三、如何自定義依賴(lài)屬性
- 1、聲明依賴(lài)屬性變量。依賴(lài)屬性的聲明都是通過(guò)public static來(lái)公開(kāi)一個(gè)靜態(tài)變量,變量的類(lèi)型必須是DependencyProperty
- 2、在屬性系統(tǒng)中進(jìn)行注冊(cè)。使用DependencyProperty.Register方法來(lái)注冊(cè)依賴(lài)屬性,或者是使用DependencyProperty.RegisterReadOnly方法來(lái)注冊(cè)
- 3、使用.NET屬性包裝依賴(lài)屬性
在類(lèi)上實(shí)現(xiàn)屬性時(shí),只要該類(lèi)派生自 DependencyObject,便可以選擇使用 DependencyProperty 標(biāo)識(shí)符來(lái)標(biāo)示屬性,從而將其設(shè)置為依賴(lài)屬性。其語(yǔ)法如下:
public static DependencyProperty TextProperty; TextProperty = DependencyProperty.Register("Text", //屬性名稱(chēng) typeof(string), //屬性類(lèi)型 typeof(TestDependencyPropertyWindow), //該屬性所有者,即將該屬性注冊(cè)到那個(gè)類(lèi)上 new PropertyMetadata("")); //屬性默認(rèn)值 public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
示例:自定義一個(gè)依賴(lài)屬性,界面包括一個(gè)TextBox和TextBlock,TextBlock上面字體的前景色隨TextBox里面輸入的顏色而改變,如果TextBox里面輸入的值可以轉(zhuǎn)換成顏色,TextBlock字體的前景色會(huì)顯示輸入的顏色值,如果不能轉(zhuǎn)換,顯示默認(rèn)的前景色。
1、在當(dāng)前項(xiàng)目里面添加一個(gè)WPF版的用戶(hù)控件,命名為“MyDependencyProperty”,在MyDependencyProperty.xaml.cs文件里面自定義一個(gè)依賴(lài)屬性:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfDemo { /// <summary> /// MyDependencyProperty.xaml 的交互邏輯 /// </summary> public partial class MyDependencyProperty : UserControl { public MyDependencyProperty() { InitializeComponent(); } //1、聲明依賴(lài)屬性變量 public static readonly DependencyProperty MyColorProperty; //2、在屬性系統(tǒng)中進(jìn)行注冊(cè) static MyDependencyProperty() { MyColorProperty = DependencyProperty.Register("MyColor", typeof(string), typeof(MyDependencyProperty), new PropertyMetadata("Red", (s, e) => { var mdp = s as MyDependencyProperty; if (mdp != null) { try { var color = (Color)ColorConverter.ConvertFromString(e.NewValue.ToString()); mdp.Foreground = new SolidColorBrush(color); } catch { mdp.Foreground = new SolidColorBrush(Colors.Black); } } })); } //3、使用.NET屬性包裝依賴(lài)屬性:屬性名稱(chēng)與注冊(cè)時(shí)候的名稱(chēng)必須一致, //即屬性名MyColor對(duì)應(yīng)注冊(cè)時(shí)的MyColor public string MyColor { get { return (string)GetValue(MyColorProperty); } set { SetValue(MyColorProperty, value); } } } }
快速定義依賴(lài)屬性的快捷方式:
輸入propdp,連續(xù)按兩下Tab健,自動(dòng)生成定義依賴(lài)屬性的語(yǔ)法。和輸入cw連續(xù)按兩下Tab健,自動(dòng)生成Console.Write()一樣。
public int MyProperty { get { return (int)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
2、在MyDependencyProperty.xaml里面添加一個(gè)TextBlock
<UserControl x:Class="WpfDemo.MyDependencyProperty" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <TextBlock>我是自定義的依賴(lài)屬性</TextBlock> </Grid> </UserControl>
3、在MainWindow.xaml里面引用新創(chuàng)建的用戶(hù)控件,并添加一個(gè)TextBox,用于輸入顏色值,并將自定義的依賴(lài)屬性MyColor綁定到TextBox
<Window x:Class="WpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:p="clr-namespace:WpfDemo" Title="依賴(lài)屬性" Height="237" Width="525" WindowStartupLocation="CenterScreen"> <Grid > <StackPanel> <TextBox Name="tbColor"></TextBox> <p:MyDependencyProperty MyColor="{Binding Path=Text,ElementName=tbColor}" ></p:MyDependencyProperty> </StackPanel> </Grid> </Window>
在設(shè)計(jì)界面顯示的效果:
4、程序運(yùn)行效果:
在TextBox里面輸入正確的顏色值,前景色會(huì)顯示為當(dāng)前輸入的顏色:
在TextBox里面輸入錯(cuò)誤的顏色值,前景色會(huì)顯示為默認(rèn)顏色:
到此這篇關(guān)于WPF依賴(lài)屬性用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息(重構(gòu)cs)
上一篇雖然它算不上是完全動(dòng)態(tài)化,但它已經(jīng)達(dá)到初期想要的效果,現(xiàn)另開(kāi)一篇,不是重新另外寫(xiě),而是想重構(gòu)cs的代碼,因?yàn)榍耙黄拇a雖然簡(jiǎn)單,但代碼冗余過(guò)多,感興趣的朋友可以參考下哈2013-03-03獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))
獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))...2007-03-03asp.net中利用ajax獲取動(dòng)態(tài)創(chuàng)建表中文本框的值
通常在做主從表的數(shù)據(jù)錄入中,會(huì)碰到在一個(gè)頁(yè)面上同時(shí)錄入主表數(shù)據(jù)和從表數(shù)據(jù),主表的數(shù)據(jù)只有一條,從表的數(shù)據(jù)有一條到多條,這樣就要?jiǎng)討B(tài)創(chuàng)建從表數(shù)據(jù)錄入入口。2010-03-03詳解如何在ASP.NET Core中應(yīng)用Entity Framework
本篇文章主要介紹了如何在ASP.NET Core中應(yīng)用Entity Framework,具有一定的參考價(jià)值,有興趣的可以一起了解一下。2016-12-12asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
有關(guān)Excel下載的文章網(wǎng)上想必有很多,利用閑暇時(shí)間整理了一些有Excel下載方法的文章,接下來(lái)介紹五種實(shí)現(xiàn)Excel下載的方法,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)Excel下載有所幫助2013-02-02ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法
這篇文章介紹了ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03asp.net core MVC 過(guò)濾器之ActionFilter過(guò)濾器(2)
這篇文章主要為大家詳細(xì)介紹了asp.net core MVC過(guò)濾器之ActionFilter過(guò)濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08asp.net 多文件上傳,兼容IE6/7/8,提供完整代碼下載
項(xiàng)目中需要使用多文件上傳,上傳的文件保存在磁盤(pán)文件上,文件相關(guān)信息保存在數(shù)據(jù)庫(kù)里。2010-07-07ASP.NET返回上一頁(yè)面的實(shí)現(xiàn)代碼
今天用ASP.NET做一個(gè)類(lèi)似淘寶中的訂單管理系統(tǒng),遇到的問(wèn)題:主界面通過(guò)一個(gè)超鏈接跳轉(zhuǎn)到子界面后,試了好長(zhǎng)時(shí)間還是無(wú)法從子界面返回主界面。2011-04-04