日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF整理-为User Control添加依赖属性

發布時間:2024/9/5 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF整理-为User Control添加依赖属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF整理-為User Control添加依賴屬性 原文:WPF整理-為User Control添加依賴屬性

依賴屬性

".NET properties are nothing more than syntactic sugar over set and get methods."

我們知道.NET的屬性只不過是get/set方法的語法糖衣。

"Dependency properties are the workhorse of WPF. This infrastructure provides for many of WPF's features, such as data binding, animations, and visual inheritance. In fact, most of the various element properties are Dependency Properties. Sometimes we need to create such properties for our own controls or windows."

依賴屬性這個基礎架構為我們提供了實現WPF諸多特性的基礎,如數據綁定、動畫等。

DependencyObject和DependencyPorperty兩個類是WPF屬性系統的核心。DependencyObject是WPF系統中相當底層的一個基類,如下:

從這顆繼承樹可以看出,WPF的所有UI控件都是依賴對象。WPF的類庫在設計時充分利用了依賴屬性的優勢,UI空間的絕大多數屬性都已經依賴化了。

有些時候,我們需要為我們自定義的類或控件等添加依賴屬性。

DebugLZQ前面的博文:WPF 自定義依賴屬性?介紹了如何為自定義的類添加依賴屬性;這篇博文以前面的博文為基礎,介紹如何為User Control添加Dependency Property。

為User Control添加依賴屬性

1.首先定義一個名為SimpleControl的UserControl;在SimpleControl.xaml.cs中鍵入"propdp"

連按2次Tab,修改依賴屬性名為YearPublishedProperty,屬性包裝名為YearPublished,默認值為2013。最終如下:

public int YearPublished {get { return (int)GetValue(YearPublishedProperty); }set { SetValue(YearPublishedProperty, value); } }public static readonly DependencyProperty YearPublishedProperty =DependencyProperty.Register("YearPublished", typeof(int), typeof(SimpleControl), new PropertyMetadata(2013));

2.為了能在XAML中使用,添加這個映射

<Window x:Class="CreatingADependencyProperty.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CreatingADependencyProperty"

我們用Binding使用如下:

<StackPanel><local:SimpleControl x:Name="_simple"/><TextBlock Text="{Binding YearPublished,ElementName=_simple}" FontSize="30"/><Button Content="Change Value" FontSize="20" Click="OnChangeValue"/></StackPanel>

Click事件如下

private void OnChangeValue(object sender, RoutedEventArgs e) {_simple.YearPublished++; }

程序運行如下:

點擊幾次button

?

posted on 2018-05-29 00:47 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/9103096.html

總結

以上是生活随笔為你收集整理的WPF整理-为User Control添加依赖属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。