目錄
控件的父類
DispatcherObject類
DependencyObject類
DependencyObject 類的關鍵成員和方法
Visual類
Visual 類的主要成員和方法
UIElement類
UIElement 類的主要成員和功能
FrameworkElement類
FrameworkElement 類的主要成員和功能
控件的父類
在 WPF (Windows Presentation Foundation) 框架中,控件的父類們形成了一個層次結構,其中最重要的父類是 DispatcherObject。雖然在整個 .NET 框架中,DispatcherObject 只是居于次要地位,但在 WPF 中卻扮演著至關重要的角色,用于處理對象與 Dispatcher 之間的關聯,確保 UI 元素的正確更新。
除了 DispatcherObject,WPF 控件的父類們還包括 DependencyObject、Visual、UIElement 和 FrameworkElement。這些父類通過多層次的繼承關系,為不同類型的控件提供了各種不同的功能和行為。
控件的繼承結構形成了一棵樹,使得 WPF 框架具有高度的靈活性和可擴展性,同時也體現了微軟工程師們在設計框架時的經典和合理的代碼架構。
DispatcherObject類
DispatcherObject 類是 WPF 中非常重要的一個基類,位于 System.Windows.Threading 命名空間中。它的作用是管理對象與 Dispatcher 之間的關聯,以確保對象在 UI 線程上正確地進行操作。在 WPF 中,UI 元素必須在創建它們的線程上進行訪問和更新,而 DispatcherObject 提供了一種機制來實現這一點。
1、Dispatcher 關聯:
- DispatcherObject 類是與 Dispatcher 相關的,Dispatcher 負責管理與線程關聯的消息隊列。每個 DispatcherObject 都與一個特定的 Dispatcher 實例相關聯,通過 Dispatcher 對象可以訪問線程相關的上下文信息,以確保對象的操作在正確的線程上執行。
2、線程安全性:
- 由于 DispatcherObject 對象與特定的 UI 線程關聯,它們可以確保對象的屬性訪問和方法調用都在正確的線程上執行,從而避免了多線程并發訪問時可能出現的問題,比如線程沖突和資源競爭。
3、UI 元素更新:
- WPF 中的大部分 UI 元素都直接或間接地繼承自 DispatcherObject,這使得它們能夠在 UI 線程上進行更新,包括屬性更改、界面重繪等操作。這樣可以確保界面的響應性和一致性。
4、異步操作:
- 通過與 Dispatcher 的關聯,DispatcherObject 還提供了一種執行異步操作的機制,可以使用 Dispatcher.Invoke 或 Dispatcher.BeginInvoke 方法來在 UI 線程上執行操作,從而避免了阻塞 UI 線程。
5、應用場景:
- DispatcherObject 主要用于 WPF 中需要與 UI 元素交互的類,比如窗口、控件、動畫等。在開發過程中,通常不需要直接實例化 DispatcherObject 類,而是通過繼承的方式來間接使用其功能。
總之,DispatcherObject 類的主要責任是管理對象與關聯的 Dispatcher 之間的關系,并提供方法來檢查和驗證訪問對象的線程權限。
- 提供對當前 Dispatcher 的訪問權限:DispatcherObject 提供了 Dispatcher 屬性,該屬性允許派生類訪問對象所關聯的當前 Dispatcher。通過這個屬性,派生類可以獲得與 UI 線程關聯的 Dispatcher 實例,并使用它來在正確的線程上更新 UI 元素。
- 提供線程訪問權限的檢查和驗證:DispatcherObject 提供了 CheckAccess 和 VerifyAccess 方法,用于檢查當前線程是否有權訪問對象。CheckAccess 方法返回一個布爾值,表示當前線程是否有權訪問對象,而 VerifyAccess 方法則在線程無權訪問對象時引發異常。通過這兩個方法,派生類可以在訪問對象之前驗證當前線程的訪問權限,從而確保對象的訪問和更新操作在正確的線程上進行。
DispatcherObject 類通過管理與 Dispatcher 的關聯,并提供線程訪問權限的檢查和驗證方法,確保對象的操作在正確的線程上執行,從而保證了 WPF 應用程序的穩定性和性能。
代碼示例:
MainWindow.xaml:
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="學習之路" Height="450" Width="800"><Grid><Button Content="啟動后臺任務" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/><TextBlock x:Name="ResultTextBlock" Text="" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,50,0,0"/></Grid>
</Window>
MainWindow.xaml.cs:?
using System;
using System.Threading;
using System.Windows;namespace WpfApp2
{/// <summary>/// 主窗口.xaml的交互邏輯/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){// 啟動一個新線程來執行后臺任務Thread thread = new Thread(BackgroundTask);thread.Start();}private void BackgroundTask(){// 模擬后臺任務Thread.Sleep(2000);// 在后臺線程中更新 UI 元素Application.Current.Dispatcher.Invoke(() =>{// 通過 DispatcherObject 更新 UI 元素ResultTextBlock.Text = "后臺任務已完成";});}}
}
當用戶點擊按鈕時,將會啟動一個后臺線程執行后臺任務,然后在任務完成后,通過 Dispatcher.Invoke 方法,在 UI 線程上更新 ResultTextBlock 的內容,以顯示任務已完成的信息。
DependencyObject類
DependencyObject 是 WPF 中非常重要的基類之一,它提供了一種依賴屬性和依賴項之間關聯的機制,是 WPF 中實現數據綁定、樣式、模板、動畫等高級功能的基礎。
- 依賴屬性:DependencyObject 支持依賴屬性的定義和使用。依賴屬性是一種特殊類型的屬性,它具有與之關聯的值,可以通過數據綁定、動畫、樣式等方式進行設置和獲取。依賴屬性的一個重要特性是其值可以在不同的元素之間共享和傳遞,從而實現了數據的自動更新和同步。
- 依賴項:DependencyObject 以及其派生類被稱為依賴項,因為它們支持依賴屬性,并且能夠參與屬性值的計算、傳遞和更新。每個 DependencyObject 實例都維護一個與之關聯的屬性表,用于存儲依賴屬性的值和相關的元數據信息。
- 線程安全性:與 DispatcherObject 不同,DependencyObject 并沒有與特定的線程關聯,因此它的操作并不受 UI 線程的限制。這意味著你可以在任何線程上創建、修改 DependencyObject 的實例,而不必擔心線程安全性問題。
- 應用場景:DependencyObject 主要用于 WPF 中的可視化元素,比如窗口、控件、布局容器等。通過使用依賴屬性,可以實現諸如綁定、動畫、樣式等豐富的 UI 功能,使得應用程序的開發變得更加靈活和高效。
- 派生類:除了直接使用 DependencyObject 外,還可以通過創建自定義的派生類來擴展其功能。通過派生類,可以定義自己的依賴屬性,并實現與其他 WPF 元素的交互和集成。
總之,DependencyObject 類是 WPF 中實現高級 UI 功能的關鍵之一,通過支持依賴屬性的定義和使用,它為 WPF 應用程序提供了豐富的數據綁定、樣式、模板、動畫等功能,為開發人員提供了強大的工具和技術來創建現代化、交互式的用戶界面。
DependencyObject 類的關鍵成員和方法
DependencyObject 類的定義包括了一系列方法和屬性,其中最常用的是 GetValue 和 SetValue 方法,用于獲取和設置依賴屬性的值。
以下是 DependencyObject 類的關鍵成員和方法:
- DependencyObjectType:獲取當前 DependencyObject 的 DependencyObjectType 對象,用于表示該對象的類型信息。
- IsSealed:獲取一個值,該值指示此 DependencyObject 是否為不可變的。當對象被封閉(sealed)時,其屬性不能被修改。
- GetValue(DependencyProperty dp):獲取指定依賴屬性的值。由于不確定屬性值的類型,因此該方法返回一個 object 類型的值。
- SetValue(DependencyProperty dp, object value):設置指定依賴屬性的值。第一個參數 dp 表示要設置的依賴屬性,第二個參數 value 表示要設置的新值。
- ClearValue(DependencyProperty dp):清除指定依賴屬性的值,將其重置為默認值。
- ClearValue(DependencyPropertyKey key):通過提供屬性的密鑰來清除指定依賴屬性的值。
- CoerceValue(DependencyProperty dp):強制重新計算指定依賴屬性的值。
- GetLocalValueEnumerator():獲取一個枚舉器,用于遍歷此對象上的所有本地屬性值。
- ReadLocalValue(DependencyProperty dp):獲取指定依賴屬性的本地值。
- SetCurrentValue(DependencyProperty dp, object value):設置依賴屬性的當前值,而不影響該屬性的原始值。
- OnPropertyChanged(DependencyPropertyChangedEventArgs e):當依賴屬性的值發生變化時調用的虛擬方法,用于在派生類中處理屬性值更改的通知。
- ShouldSerializeProperty(DependencyProperty dp):確定指定的依賴屬性是否應該在序列化時進行持久化。
總之,DependencyObject 類提供了一系列方法和屬性,用于管理依賴屬性的值,以及處理屬性值的變化通知。
代碼示例:
MainWindow.xaml:
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="學習之路" Height="450" Width="800"><Grid><Button Content="點我" Click="Button_Click" Margin="327,192,327,192"/></Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;namespace WpfApp2
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){// 創建 CustomDependencyObject 實例CustomDependencyObject obj = new CustomDependencyObject();// 設置依賴屬性的值obj.SetValue(CustomDependencyObject.CustomProperty, "你好,依賴屬性!");// 獲取依賴屬性的值并顯示在消息框中MessageBox.Show(obj.GetValue(CustomDependencyObject.CustomProperty).ToString());}}
}
CustomDependencyObject.cs:
using System.Windows;namespace WpfApp2
{public class CustomDependencyObject : DependencyObject{// 創建一個依賴屬性public static readonly DependencyProperty CustomProperty =DependencyProperty.Register("Custom", typeof(string), typeof(CustomDependencyObject));// 屬性包裝器public string Custom{get { return (string)GetValue(CustomProperty); }set { SetValue(CustomProperty, value); }}}
}
Visual類
Visual 類是 WPF 中表示可視化對象的基類之一,它提供了一種用于呈現和渲染圖形元素的機制。Visual 類本身并不直接表示 UI 元素,而是為 UI 元素的呈現提供了基礎支持。
以下是關于 Visual 類的詳細信息:
- 圖形呈現:Visual 類定義了一組基本的圖形呈現功能,包括繪制、渲染、布局等。所有可視化對象都可以通過繼承 Visual 類來實現自定義的圖形呈現邏輯。換句話說,將來我們要學習的Button、TextBox、CheckBox、Gird、ListBox等所有控件都繼承自Visual類,控件在繪制到界面的過程中,涉及到轉換、裁剪、邊框計算等功能,都是使用了Visual父類的功能。
- 基類:Visual 類是所有可視化對象的基類,包括窗口、控件、形狀等。通過繼承 Visual 類,可以為自定義的可視化對象提供統一的呈現和渲染接口。
- 高性能繪制:Visual 類提供了一種高性能的繪制和渲染機制,可以有效地處理大量的圖形元素,并在需要時進行異步渲染和重繪。
- 圖形樹結構:Visual 類以及其派生類組成了 WPF 中的圖形樹結構,每個可視化對象都是圖形樹中的一個節點。通過對圖形樹的操作,可以實現圖形元素的組合、變換、剪裁等操作。
- 布局和排列:Visual 類支持布局和排列功能,可以通過設置位置、大小、旋轉等屬性來控制圖形元素的位置和外觀。
- 事件處理:Visual 類支持事件處理機制,可以捕獲和處理用戶輸入、鼠標事件、鍵盤事件等。
Visual 類的主要成員和方法
Visual 類是 WPF 中的一個核心類,它作為所有可視化對象的基本抽象,提供了許多屬性和方法來支持對象的呈現和交互。讓我們來看一下 Visual 類的主要成員和方法:
屬性:
- VisualParent:獲取可視對象的可視化父對象。
- VisualChildrenCount:獲取當前對象的子元素數量。
- VisualOffset:獲取或設置當前可視對象的偏移量值。
- VisualOpacity:獲取或設置可視對象的不透明度。
- VisualEffect:獲取或設置要應用于可視對象的位圖效果。
- VisualTransform:獲取或設置可視對象的變換效果。
其他屬性如 VisualYSnappingGuidelines、VisualClip 等,用于控制可視對象的剪裁、緩存模式、邊框等。
方法:
- FindCommonVisualAncestor(DependencyObject otherVisual):返回兩個可視對象的公共上級。
- IsAncestorOf(DependencyObject descendant):確定可視對象是否為指定對象的上級。
- IsDescendantOf(DependencyObject ancestor):確定可視對象是否為指定對象的后代。
- PointFromScreen(Point point):將屏幕坐標中的點轉換為表示當前坐標系的可視對象的坐標。
- PointToScreen(Point point):將表示當前坐標系的可視對象的點轉換為屏幕坐標中的點。
- TransformToAncestor(Visual ancestor):返回一個轉換,用于將當前可視對象的坐標轉換為指定可視對象的坐標。
- TransformToDescendant(Visual descendant):返回一個轉換,用于將當前可視對象的坐標轉換為指定后代可視對象的坐標。
- TransformToVisual(Visual visual):返回一個轉換,用于將當前可視對象的坐標轉換為指定可視對象的坐標。
其他方法如 HitTestCore、OnVisualChildrenChanged 等,用于執行命中測試、處理可視對象的子元素變化等。
總之,Visual 類是 WPF 中表示可視化對象的基類,它提供了一種用于呈現和渲染圖形元素的基礎支持,是實現復雜 UI 功能的關鍵之一。通過繼承 Visual 類,可以實現自定義的可視化對象,并在 WPF 應用程序中實現豐富的圖形用戶界面。那么,誰又去繼承了Visual類? 答案是UIElement類。
UIElement類
UIElement 類是 WPF 中所有用戶界面元素的基類,它定義了許多基本的用戶界面行為和特性。讓我們來了解一下 UIElement 類的特點和功能:
- 呈現和布局:UIElement 類負責定義用戶界面元素的呈現和布局。它可以繪制自己的內容,并且可以根據布局算法安排自己的位置和大小。
- 輸入事件處理:UIElement 類處理用戶輸入事件,如鼠標點擊、鍵盤輸入、觸摸操作等。它定義了一系列方法和事件來響應這些輸入事件,比如 MouseDown、MouseUp、KeyDown、KeyUp 等。
- 焦點管理:UIElement 類支持焦點管理,可以接收鍵盤焦點和鼠標焦點。它定義了一些方法和事件來處理焦點的獲取和丟失,如 GotFocus、LostFocus 等。
- 布局變換:UIElement 類可以通過設置 RenderTransform 屬性來實現布局的變換,比如旋轉、縮放、平移等。
- 命中測試:UIElement 類支持命中測試,用于確定鼠標點擊或觸摸操作是否命中了該元素。它定義了 HitTest 方法和 IsHitTestVisible 屬性來控制命中測試的行為。
- 動畫和效果:UIElement 類可以應用動畫和效果來改變其外觀和行為。它定義了 BeginAnimation 方法和 Effect 屬性來支持動畫和效果的應用。
- 事件處理:UIElement 類定義了一系列路由事件,可以在元素樹中傳播和處理。這些事件包括鼠標事件、鍵盤事件、觸摸事件等,以及一些通用的命令事件。
- 可視化樹管理:UIElement 類可以組織成可視化樹,從而構建用戶界面的層次結構。它定義了 Parent 屬性和一些方法來管理可視化樹的結構。
總之,UIElement 類是 WPF 中所有用戶界面元素的基礎,它提供了許多基本的用戶界面行為和特性,包括呈現、布局、輸入事件處理、焦點管理、布局變換、命中測試、動畫和效果、事件處理以及可視化樹管理等。通過繼承 UIElement 類,開發人員可以輕松地創建各種自定義的用戶界面元素,并實現豐富的用戶界面交互。
UIElement 類的主要成員和功能
UIElement 類是 WPF 中非常重要的一個基類,它繼承自 Visual 類,提供了許多用于創建用戶界面元素的基本功能。
我們先來看一下這個類的結構定義。
public class UIElement : Visual, IAnimatable, IInputElement
{
public static readonly RoutedEvent PreviewMouseDownEvent;
public static readonly DependencyProperty AreAnyTouchesOverProperty;
public static readonly DependencyProperty AreAnyTouchesDirectlyOverProperty;
public static readonly DependencyProperty IsKeyboardFocusedProperty;
public static readonly DependencyProperty IsStylusCaptureWithinProperty;
public static readonly DependencyProperty IsStylusCapturedProperty;
public static readonly DependencyProperty IsMouseCaptureWithinProperty;
public static readonly DependencyProperty IsMouseCapturedProperty;
public static readonly DependencyProperty IsKeyboardFocusWithinProperty;
public static readonly DependencyProperty IsStylusOverProperty;
public static readonly DependencyProperty IsMouseOverProperty;
public static readonly DependencyProperty IsMouseDirectlyOverProperty;
public static readonly RoutedEvent TouchLeaveEvent;
public static readonly RoutedEvent TouchEnterEvent;
public static readonly RoutedEvent LostTouchCaptureEvent;
public static readonly RoutedEvent GotTouchCaptureEvent;
public static readonly RoutedEvent TouchUpEvent;
public static readonly RoutedEvent PreviewTouchUpEvent;
public static readonly RoutedEvent TouchMoveEvent;
public static readonly RoutedEvent PreviewTouchMoveEvent;
public static readonly RoutedEvent TouchDownEvent;
public static readonly RoutedEvent PreviewTouchDownEvent;
public static readonly RoutedEvent DropEvent;
public static readonly RoutedEvent PreviewDropEvent;
public static readonly RoutedEvent DragLeaveEvent;
public static readonly RoutedEvent PreviewDragLeaveEvent;
public static readonly DependencyProperty AreAnyTouchesCapturedProperty;
public static readonly DependencyProperty AreAnyTouchesCapturedWithinProperty;
public static readonly DependencyProperty AllowDropProperty;
public static readonly DependencyProperty RenderTransformProperty;
public static readonly RoutedEvent ManipulationCompletedEvent;
public static readonly RoutedEvent ManipulationBoundaryFeedbackEvent;
public static readonly RoutedEvent ManipulationInertiaStartingEvent;
public static readonly RoutedEvent ManipulationDeltaEvent;
public static readonly RoutedEvent ManipulationStartedEvent;
public static readonly RoutedEvent ManipulationStartingEvent;
public static readonly DependencyProperty IsManipulationEnabledProperty;
public static readonly DependencyProperty FocusableProperty;
public static readonly DependencyProperty IsVisibleProperty;
public static readonly DependencyProperty IsHitTestVisibleProperty;
public static readonly DependencyProperty IsEnabledProperty;
public static readonly DependencyProperty IsFocusedProperty;
public static readonly RoutedEvent DragOverEvent;
public static readonly RoutedEvent LostFocusEvent;
public static readonly DependencyProperty SnapsToDevicePixelsProperty;
public static readonly DependencyProperty ClipProperty;
public static readonly DependencyProperty ClipToBoundsProperty;
public static readonly DependencyProperty VisibilityProperty;
public static readonly DependencyProperty UidProperty;
public static readonly DependencyProperty CacheModeProperty;
public static readonly DependencyProperty BitmapEffectInputProperty;
public static readonly DependencyProperty EffectProperty;
public static readonly DependencyProperty BitmapEffectProperty;
public static readonly DependencyProperty OpacityMaskProperty;
public static readonly DependencyProperty OpacityProperty;
public static readonly DependencyProperty RenderTransformOriginProperty;
public static readonly RoutedEvent GotFocusEvent;
public static readonly RoutedEvent PreviewDragOverEvent;
public static readonly DependencyProperty IsStylusDirectlyOverProperty;
public static readonly RoutedEvent PreviewDragEnterEvent;
public static readonly RoutedEvent StylusMoveEvent;
public static readonly RoutedEvent PreviewStylusMoveEvent;
public static readonly RoutedEvent StylusUpEvent;
public static readonly RoutedEvent PreviewStylusUpEvent;
public static readonly RoutedEvent StylusDownEvent;
public static readonly RoutedEvent PreviewStylusDownEvent;
public static readonly RoutedEvent QueryCursorEvent;
public static readonly RoutedEvent LostMouseCaptureEvent;
public static readonly RoutedEvent GotMouseCaptureEvent;
public static readonly RoutedEvent MouseLeaveEvent;
public static readonly RoutedEvent MouseEnterEvent;
public static readonly RoutedEvent MouseWheelEvent;
public static readonly RoutedEvent PreviewStylusInAirMoveEvent;
public static readonly RoutedEvent PreviewMouseWheelEvent;
public static readonly RoutedEvent PreviewMouseMoveEvent;
public static readonly RoutedEvent MouseRightButtonUpEvent;
public static readonly RoutedEvent PreviewMouseRightButtonUpEvent;
public static readonly RoutedEvent MouseRightButtonDownEvent;
public static readonly RoutedEvent PreviewMouseRightButtonDownEvent;
public static readonly RoutedEvent DragEnterEvent;
public static readonly RoutedEvent PreviewMouseLeftButtonUpEvent;
public static readonly RoutedEvent MouseLeftButtonDownEvent;
public static readonly RoutedEvent PreviewMouseLeftButtonDownEvent;
public static readonly RoutedEvent MouseUpEvent;
public static readonly RoutedEvent PreviewMouseUpEvent;
public static readonly RoutedEvent MouseDownEvent;
public static readonly RoutedEvent MouseMoveEvent;
public static readonly RoutedEvent StylusInAirMoveEvent;
public static readonly RoutedEvent MouseLeftButtonUpEvent;
public static readonly RoutedEvent StylusLeaveEvent;
public static readonly RoutedEvent StylusEnterEvent;
public static readonly RoutedEvent GiveFeedbackEvent;
public static readonly RoutedEvent PreviewGiveFeedbackEvent;
public static readonly RoutedEvent QueryContinueDragEvent;
public static readonly RoutedEvent TextInputEvent;
public static readonly RoutedEvent PreviewTextInputEvent;
public static readonly RoutedEvent LostKeyboardFocusEvent;
public static readonly RoutedEvent PreviewLostKeyboardFocusEvent;
public static readonly RoutedEvent GotKeyboardFocusEvent;
public static readonly RoutedEvent PreviewGotKeyboardFocusEvent;
public static readonly RoutedEvent KeyUpEvent;
public static readonly RoutedEvent PreviewKeyUpEvent;
public static readonly RoutedEvent KeyDownEvent;
public static readonly RoutedEvent PreviewQueryContinueDragEvent;
public static readonly RoutedEvent PreviewStylusButtonUpEvent;
public static readonly RoutedEvent PreviewKeyDownEvent;
public static readonly RoutedEvent StylusInRangeEvent;
public static readonly RoutedEvent PreviewStylusInRangeEvent;
public static readonly RoutedEvent StylusOutOfRangeEvent;
public static readonly RoutedEvent PreviewStylusSystemGestureEvent;
public static readonly RoutedEvent PreviewStylusOutOfRangeEvent;
public static readonly RoutedEvent GotStylusCaptureEvent;
public static readonly RoutedEvent LostStylusCaptureEvent;
public static readonly RoutedEvent StylusButtonDownEvent;
public static readonly RoutedEvent StylusButtonUpEvent;
public static readonly RoutedEvent PreviewStylusButtonDownEvent;
public static readonly RoutedEvent StylusSystemGestureEvent;public UIElement();public string Uid { get; set; }
public Visibility Visibility { get; set; }
public bool ClipToBounds { get; set; }
public Geometry Clip { get; set; }
public bool SnapsToDevicePixels { get; set; }
public bool IsFocused { get; }
public bool IsEnabled { get; set; }
public bool IsHitTestVisible { get; set; }
public bool IsVisible { get; }
public bool AreAnyTouchesCapturedWithin { get; }
public int PersistId { get; }
public bool IsManipulationEnabled { get; set; }
public bool AreAnyTouchesOver { get; }
public bool AreAnyTouchesDirectlyOver { get; }
public bool AreAnyTouchesCaptured { get; }
public IEnumerable<TouchDevice> TouchesCaptured { get; }
public IEnumerable<TouchDevice> TouchesCapturedWithin { get; }
public IEnumerable<TouchDevice> TouchesOver { get; }
public CacheMode CacheMode { get; set; }
public bool Focusable { get; set; }
public BitmapEffectInput BitmapEffectInput { get; set; }
public bool IsMouseDirectlyOver { get; }
public BitmapEffect BitmapEffect { get; set; }
public Size RenderSize { get; set; }
public bool IsArrangeValid { get; }
public bool IsMeasureValid { get; }
public Size DesiredSize { get; }
public bool AllowDrop { get; set; }
public CommandBindingCollection CommandBindings { get; }
public InputBindingCollection InputBindings { get; }
public bool HasAnimatedProperties { get; }
public bool IsMouseOver { get; }
public Effect Effect { get; set; }
public bool IsStylusOver { get; }
public bool IsMouseCaptured { get; }
public bool IsMouseCaptureWithin { get; }
public bool IsStylusDirectlyOver { get; }
public bool IsStylusCaptured { get; }
public bool IsStylusCaptureWithin { get; }
public bool IsKeyboardFocused { get; }
public bool IsInputMethodEnabled { get; }
public double Opacity { get; set; }
public Brush OpacityMask { get; set; }
public bool IsKeyboardFocusWithin { get; }
public IEnumerable<TouchDevice> TouchesDirectlyOver { get; }
public Point RenderTransformOrigin { get; set; }
public Transform RenderTransform { get; set; }
protected StylusPlugInCollection StylusPlugIns { get; }
protected virtual bool IsEnabledCore { get; }
protected internal virtual bool HasEffectiveKeyboardFocus { get; }public event KeyEventHandler KeyUp;
public event EventHandler<TouchEventArgs> TouchMove;
public event EventHandler<TouchEventArgs> PreviewTouchMove;
public event EventHandler<TouchEventArgs> TouchDown;
public event EventHandler<TouchEventArgs> PreviewTouchDown;
public event DragEventHandler Drop;
public event DragEventHandler PreviewDrop;
public event DragEventHandler DragLeave;
public event DragEventHandler PreviewDragLeave;
public event DragEventHandler DragOver;
public event DragEventHandler PreviewDragOver;
public event DragEventHandler DragEnter;
public event DragEventHandler PreviewDragEnter;
public event GiveFeedbackEventHandler GiveFeedback;
public event GiveFeedbackEventHandler PreviewGiveFeedback;
public event QueryContinueDragEventHandler QueryContinueDrag;
public event QueryContinueDragEventHandler PreviewQueryContinueDrag;
public event TextCompositionEventHandler TextInput;
public event EventHandler<TouchEventArgs> PreviewTouchUp;
public event EventHandler<TouchEventArgs> TouchUp;
public event EventHandler<TouchEventArgs> LostTouchCapture;
public event TextCompositionEventHandler PreviewTextInput;
public event EventHandler<ManipulationInertiaStartingEventArgs> ManipulationInertiaStarting;
public event EventHandler<ManipulationDeltaEventArgs> ManipulationDelta;
public event EventHandler<ManipulationStartedEventArgs> ManipulationStarted;
public event EventHandler<ManipulationStartingEventArgs> ManipulationStarting;
public event DependencyPropertyChangedEventHandler FocusableChanged;
public event DependencyPropertyChangedEventHandler IsVisibleChanged;
public event DependencyPropertyChangedEventHandler IsHitTestVisibleChanged;
public event DependencyPropertyChangedEventHandler IsEnabledChanged;
public event RoutedEventHandler LostFocus;
public event EventHandler<TouchEventArgs> GotTouchCapture;
public event RoutedEventHandler GotFocus;
public event DependencyPropertyChangedEventHandler IsKeyboardFocusedChanged;
public event DependencyPropertyChangedEventHandler IsStylusCaptureWithinChanged;
public event DependencyPropertyChangedEventHandler IsStylusDirectlyOverChanged;
public event DependencyPropertyChangedEventHandler IsMouseCaptureWithinChanged;
public event DependencyPropertyChangedEventHandler IsMouseCapturedChanged;
public event DependencyPropertyChangedEventHandler IsKeyboardFocusWithinChanged;
public event DependencyPropertyChangedEventHandler IsMouseDirectlyOverChanged;
public event EventHandler<TouchEventArgs> TouchLeave;
public event EventHandler<TouchEventArgs> TouchEnter;
public event EventHandler LayoutUpdated;
public event KeyboardFocusChangedEventHandler LostKeyboardFocus;
public event KeyboardFocusChangedEventHandler PreviewLostKeyboardFocus;
public event KeyboardFocusChangedEventHandler GotKeyboardFocus;
public event StylusEventHandler PreviewStylusMove;
public event StylusEventHandler StylusMove;
public event StylusEventHandler PreviewStylusInAirMove;
public event StylusEventHandler StylusInAirMove;
public event StylusEventHandler StylusEnter;
public event StylusEventHandler StylusLeave;
public event StylusEventHandler PreviewStylusInRange;
public event StylusEventHandler StylusInRange;
public event StylusEventHandler PreviewStylusOutOfRange;
public event StylusEventHandler StylusOutOfRange;
public event StylusSystemGestureEventHandler PreviewStylusSystemGesture;
public event StylusSystemGestureEventHandler StylusSystemGesture;
public event StylusEventHandler GotStylusCapture;
public event StylusEventHandler LostStylusCapture;
public event StylusButtonEventHandler StylusButtonDown;
public event StylusButtonEventHandler StylusButtonUp;
public event StylusButtonEventHandler PreviewStylusButtonDown;
public event StylusButtonEventHandler PreviewStylusButtonUp;
public event KeyEventHandler PreviewKeyDown;
public event KeyEventHandler KeyDown;
public event KeyEventHandler PreviewKeyUp;
public event StylusEventHandler StylusUp;
public event KeyboardFocusChangedEventHandler PreviewGotKeyboardFocus;
public event StylusEventHandler PreviewStylusUp;
public event StylusDownEventHandler PreviewStylusDown;
public event MouseButtonEventHandler PreviewMouseDown;
public event MouseButtonEventHandler MouseDown;
public event MouseButtonEventHandler PreviewMouseUp;
public event MouseButtonEventHandler MouseUp;
public event MouseButtonEventHandler PreviewMouseLeftButtonDown;
public event MouseButtonEventHandler MouseLeftButtonDown;
public event MouseButtonEventHandler PreviewMouseLeftButtonUp;
public event MouseButtonEventHandler MouseLeftButtonUp;
public event MouseButtonEventHandler PreviewMouseRightButtonDown;
public event MouseButtonEventHandler MouseRightButtonDown;
public event MouseButtonEventHandler PreviewMouseRightButtonUp;
public event MouseButtonEventHandler MouseRightButtonUp;
public event MouseEventHandler PreviewMouseMove;
public event MouseEventHandler MouseMove;
public event MouseWheelEventHandler PreviewMouseWheel;
public event MouseWheelEventHandler MouseWheel;
public event MouseEventHandler MouseEnter;
public event MouseEventHandler MouseLeave;
public event MouseEventHandler GotMouseCapture;
public event MouseEventHandler LostMouseCapture;
public event QueryCursorEventHandler QueryCursor;
public event StylusDownEventHandler StylusDown;
public event DependencyPropertyChangedEventHandler IsStylusCapturedChanged;
public event EventHandler<ManipulationCompletedEventArgs> ManipulationCompleted;
public event EventHandler<ManipulationBoundaryFeedbackEventArgs> ManipulationBoundaryFeedback;public void AddHandler(RoutedEvent routedEvent, Delegate handler);
public void AddHandler(RoutedEvent routedEvent, Delegate handler, bool handledEventsToo);
public void AddToEventRoute(EventRoute route, RoutedEventArgs e);
public void ApplyAnimationClock(DependencyProperty dp, AnimationClock clock, HandoffBehavior handoffBehavior);
public void ApplyAnimationClock(DependencyProperty dp, AnimationClock clock);
public void Arrange(Rect finalRect);
public void BeginAnimation(DependencyProperty dp, AnimationTimeline animation, HandoffBehavior handoffBehavior);
public void BeginAnimation(DependencyProperty dp, AnimationTimeline animation);
public bool CaptureMouse();
public bool CaptureStylus();
public bool CaptureTouch(TouchDevice touchDevice);
public bool Focus();
public object GetAnimationBaseValue(DependencyProperty dp);
public IInputElement InputHitTest(Point point);
public void InvalidateArrange();
public void InvalidateMeasure();
public void InvalidateVisual();
public void Measure(Size availableSize);
public virtual bool MoveFocus(TraversalRequest request);
public virtual DependencyObject PredictFocus(FocusNavigationDirection direction);
public void RaiseEvent(RoutedEventArgs e);
public void ReleaseAllTouchCaptures();
public void ReleaseMouseCapture();
public void ReleaseStylusCapture();
public bool ReleaseTouchCapture(TouchDevice touchDevice);
public void RemoveHandler(RoutedEvent routedEvent, Delegate handler);
public bool ShouldSerializeCommandBindings();
public bool ShouldSerializeInputBindings();
public Point TranslatePoint(Point point, UIElement relativeTo);
public void UpdateLayout();
protected virtual void ArrangeCore(Rect finalRect);
protected virtual Geometry GetLayoutClip(Size layoutSlotSize);
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters);
protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters);
protected virtual Size MeasureCore(Size availableSize);
protected virtual void OnAccessKey(AccessKeyEventArgs e);
protected virtual void OnChildDesiredSizeChanged(UIElement child);
protected virtual AutomationPeer OnCreateAutomationPeer();
protected virtual void OnDragEnter(DragEventArgs e);
protected virtual void OnDragLeave(DragEventArgs e);
protected virtual void OnDragOver(DragEventArgs e);
protected virtual void OnDrop(DragEventArgs e);
protected virtual void OnGiveFeedback(GiveFeedbackEventArgs e);
protected virtual void OnGotFocus(RoutedEventArgs e);
protected virtual void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e);
protected virtual void OnGotMouseCapture(MouseEventArgs e);
protected virtual void OnGotStylusCapture(StylusEventArgs e);
protected virtual void OnGotTouchCapture(TouchEventArgs e);
protected virtual void OnIsKeyboardFocusedChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsMouseCapturedChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsMouseCaptureWithinChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsStylusCapturedChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsStylusCaptureWithinChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnIsStylusDirectlyOverChanged(DependencyPropertyChangedEventArgs e);
protected virtual void OnKeyDown(KeyEventArgs e);
protected virtual void OnKeyUp(KeyEventArgs e);
protected virtual void OnLostFocus(RoutedEventArgs e);
protected virtual void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e);
protected virtual void OnLostMouseCapture(MouseEventArgs e);
protected virtual void OnLostStylusCapture(StylusEventArgs e);
protected virtual void OnLostTouchCapture(TouchEventArgs e);
protected virtual void OnManipulationBoundaryFeedback(ManipulationBoundaryFeedbackEventArgs e);
protected virtual void OnManipulationCompleted(ManipulationCompletedEventArgs e);
protected virtual void OnManipulationDelta(ManipulationDeltaEventArgs e);
protected virtual void OnManipulationInertiaStarting(ManipulationInertiaStartingEventArgs e);
protected virtual void OnManipulationStarted(ManipulationStartedEventArgs e);
protected virtual void OnManipulationStarting(ManipulationStartingEventArgs e);
protected virtual void OnMouseDown(MouseButtonEventArgs e);
protected virtual void OnMouseEnter(MouseEventArgs e);
protected virtual void OnMouseLeave(MouseEventArgs e);
protected virtual void OnMouseLeftButtonDown(MouseButtonEventArgs e);
protected virtual void OnMouseLeftButtonUp(MouseButtonEventArgs e);
protected virtual void OnMouseMove(MouseEventArgs e);
protected virtual void OnMouseRightButtonDown(MouseButtonEventArgs e);
protected virtual void OnMouseRightButtonUp(MouseButtonEventArgs e);
protected virtual void OnMouseUp(MouseButtonEventArgs e);
protected virtual void OnMouseWheel(MouseWheelEventArgs e);
protected virtual void OnPreviewDragEnter(DragEventArgs e);
protected virtual void OnPreviewDragLeave(DragEventArgs e);
protected virtual void OnPreviewDragOver(DragEventArgs e);
protected virtual void OnPreviewDrop(DragEventArgs e);
protected virtual void OnPreviewGiveFeedback(GiveFeedbackEventArgs e);
protected virtual void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e);
protected virtual void OnPreviewKeyDown(KeyEventArgs e);
protected virtual void OnPreviewKeyUp(KeyEventArgs e);
protected virtual void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e);
protected virtual void OnPreviewMouseDown(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseMove(MouseEventArgs e);
protected virtual void OnPreviewMouseRightButtonDown(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseRightButtonUp(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseUp(MouseButtonEventArgs e);
protected virtual void OnPreviewMouseWheel(MouseWheelEventArgs e);
protected virtual void OnPreviewQueryContinueDrag(QueryContinueDragEventArgs e);
protected virtual void OnPreviewStylusButtonDown(StylusButtonEventArgs e);
protected virtual void OnPreviewStylusButtonUp(StylusButtonEventArgs e);
protected virtual void OnPreviewStylusDown(StylusDownEventArgs e);
protected virtual void OnPreviewStylusInAirMove(StylusEventArgs e);
protected virtual void OnPreviewStylusInRange(StylusEventArgs e);
protected virtual void OnPreviewStylusMove(StylusEventArgs e);
protected virtual void OnPreviewStylusOutOfRange(StylusEventArgs e);
protected virtual void OnPreviewStylusSystemGesture(StylusSystemGestureEventArgs e);
protected virtual void OnPreviewStylusUp(StylusEventArgs e);
protected virtual void OnPreviewTextInput(TextCompositionEventArgs e);
protected virtual void OnPreviewTouchDown(TouchEventArgs e);
protected virtual void OnPreviewTouchMove(TouchEventArgs e);
protected virtual void OnPreviewTouchUp(TouchEventArgs e);
protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e);
protected virtual void OnQueryCursor(QueryCursorEventArgs e);
protected virtual void OnRender(DrawingContext drawingContext);
protected virtual void OnStylusButtonDown(StylusButtonEventArgs e);
protected virtual void OnStylusButtonUp(StylusButtonEventArgs e);
protected virtual void OnStylusDown(StylusDownEventArgs e);
protected virtual void OnStylusEnter(StylusEventArgs e);
protected virtual void OnStylusInAirMove(StylusEventArgs e);
protected virtual void OnStylusInRange(StylusEventArgs e);
protected virtual void OnStylusLeave(StylusEventArgs e);
protected virtual void OnStylusMove(StylusEventArgs e);
protected virtual void OnStylusOutOfRange(StylusEventArgs e);
protected virtual void OnStylusSystemGesture(StylusSystemGestureEventArgs e);
protected virtual void OnStylusUp(StylusEventArgs e);
protected virtual void OnTextInput(TextCompositionEventArgs e);
protected virtual void OnTouchDown(TouchEventArgs e);
protected virtual void OnTouchEnter(TouchEventArgs e);
protected virtual void OnTouchLeave(TouchEventArgs e);
protected virtual void OnTouchMove(TouchEventArgs e);
protected virtual void OnTouchUp(TouchEventArgs e);
protected internal virtual DependencyObject GetUIParentCore();
protected internal virtual void OnRenderSizeChanged(SizeChangedInfo info);
protected internal override void OnVisualParentChanged(DependencyObject oldParent);}
下面是 UIElement 類的主要成員和功能:
屬性:
- IsHitTestVisible:獲取或設置一個值,該值指示在命中測試過程中此元素是否可見。如果設置為 false,則該元素不會參與命中測試。
- Opacity:獲取或設置此元素的不透明度。值為 1.0 表示完全不透明,值為 0.0 表示完全透明。
- RenderTransform:獲取或設置一個轉換,該轉換在呈現此元素時應用于該元素。
- RenderTransformOrigin:獲取或設置用于呈現此元素的任何呈現轉換的中心點。
- Visibility:獲取或設置此元素在用戶界面中的可見性狀態。可見性狀態可以是 Visible、Collapsed 或 Hidden。
方法:
- CaptureMouse():捕獲鼠標,使此元素接收鼠標事件,即使鼠標指針已移出元素的邊界。
- ReleaseMouseCapture():釋放先前由此元素捕獲的鼠標。
事件:
- MouseDown、MouseUp:鼠標按下和釋放事件。
- MouseEnter、MouseLeave:鼠標進入和離開事件。
- MouseMove:鼠標移動事件。
- GotFocus、LostFocus:元素獲得和失去焦點事件。
命中測試:
- HitTestVisibility 屬性和 HitTestBounds 方法用于執行命中測試,判斷鼠標點擊是否命中該元素。
- 輸入事件處理:
- PreviewKeyDown、KeyDown、PreviewKeyUp、KeyUp 事件用于處理鍵盤輸入。
- PreviewMouseDown、MouseDown、PreviewMouseUp、MouseUp、PreviewMouseMove、MouseMove 等事件用于處理鼠標輸入。
布局和渲染:
- Measure(Size availableSize) 和 Arrange(Rect finalRect) 方法用于計算元素的大小和位置。
- InvalidateMeasure() 和 InvalidateArrange() 方法用于標記元素的布局無效,需要重新計算。
焦點管理:
- Focus() 方法用于將焦點設置到該元素上。
- IsFocused 屬性表示該元素是否具有焦點。
總之,UIElement 類提供了許多基本的用戶界面功能,包括輸入事件處理、命中測試、布局和渲染、焦點管理等。通過繼承 UIElement 類,可以創建各種自定義的用戶界面元素,并實現豐富的用戶界面交互。
代碼示例:
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="學習之路" Height="450" Width="800"><Grid><Button x:Name="myButton" Content="點一下" Click="myButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/></Grid>
</Window>
using System.Windows;namespace WpfApp2
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}// 在代碼中使用 Button,它是 UIElement 的子類private void myButton_Click(object sender, RoutedEventArgs e){MessageBox.Show("按鈕點擊了!");}}
}
FrameworkElement類
FrameworkElement 是 WPF 中控件體系的核心基類之一,它繼承自 UIElement,而 UIElement 是更基礎的用戶界面元素基類,提供了諸如輸入事件、布局和呈現等基本功能。繼承關系是:Object->DispatcherObject->DependencyObject->Visual->UIElement->FrameworkElement。
在 FrameworkElement 基類之上,控件體系開始分支,形成三個主要方向:
- Shape 圖形類:這個方向包括一系列的形狀類,如 Rectangle、Ellipse、Polygon 等,它們繼承自 Shape 類,Shape 類繼承自 FrameworkElement,因此它們具有 FrameworkElement 的所有功能,同時還有特定于形狀的屬性和方法。
- Control 控件類:這個方向包括一系列的用戶界面控件類,如 Button、TextBox、ComboBox 等,它們繼承自 Control 類,Control 類繼承自 FrameworkElement,因此它們也具有 FrameworkElement 的所有功能,同時還有特定于控件的屬性和方法。
- Panel 布局類:這個方向包括一系列的布局容器類,如 StackPanel、Grid、Canvas 等,它們繼承自 Panel 類,Panel 類繼承自 FrameworkElement,因此它們也具有 FrameworkElement 的所有功能,同時還有特定于布局的屬性和方法。
根據官方文檔,FrameworkElement 在 WPF 框架中具有以下主要功能和責任:
- 布局系統定義:FrameworkElement 提供了特定于 WPF 框架級別的布局系統實現,包括為派生類提供替代布局方法的密封方法。例如,FrameworkElement 提供了 ArrangeOverride 方法,用于派生類重寫以提供自定義的排列邏輯。這些更改反映了在 WPF 框架級別存在一個完整的布局系統,可以呈現任何 FrameworkElement 派生類。
- 邏輯樹:FrameworkElement 支持將元素樹表示為邏輯樹,并支持在標記中定義該樹。然而,FrameworkElement 故意不定義內容模型,而是將該責任留給派生類。
- 對象生存期事件:FrameworkElement 定義了多個與對象生存期相關的事件,例如 Initialized、Loaded 和 Unloaded 事件。這些事件提供了在元素初始化或加載到邏輯樹中時執行代碼的掛鉤。
- 支持數據綁定和動態資源引用:FrameworkElement 實現了解析存儲為表達式的成員值的能力,這對于支持數據綁定和動態資源引用非常重要。數據綁定和資源的屬性級支持由 DependencyProperty 類實現,并在屬性系統中體現。
- 風格:FrameworkElement 定義了 Style 屬性,用于應用樣式。但是,它并未定義對模板的支持或支持修飾符。這些功能通常由控件類引入,如 Control 和 ContentControl。
- 更多動畫支持:FrameworkElement 通過實現 BeginStoryboard 等成員擴展了某些動畫支持,使得在界面元素上可以更方便地應用動畫效果。
總之,FrameworkElement 在 WPF 框架中承擔了多項重要責任,包括布局系統的實現、邏輯樹的管理、對象生存期事件的處理、數據綁定和動態資源引用的支持、樣式應用以及動畫支持等。這些功能使得 FrameworkElement 成為 WPF 控件體系中的核心基類之一。
FrameworkElement 類的主要成員和功能
屬性分析:
1. LayoutTransform 屬性:LayoutTransform 屬性用于在執行布局時應用于元素的圖形轉換。與 RenderTransform 屬性不同,LayoutTransform 在布局時應用,而 RenderTransform 在布局后應用。通常建議使用 RenderTransform,因為它的性能更好。
2. Width 和 Height 屬性:Width 和 Height 屬性分別表示控件的寬度和高度。ActualWidth 和 ActualHeight 屬性表示控件的實際呈現寬度和高度,而 MaxWidth 和 MinWidth、MaxHeight 和 MinHeight 屬性用于設置控件的最大和最小寬度和高度限制。
3. Tag 屬性:Tag 屬性用于在控件上存儲任意對象。它是一個 object 類型的屬性,可用于臨時存儲與控件相關的數據。
4. Name 屬性:Name 屬性用于設置控件的標識名稱,在 XAML 中引用控件時會使用到。
5. Margin 屬性:Margin 屬性用于設置控件的外邊距,指定控件與其容器邊界之間的間距。
6. Padding 屬性:Padding 屬性用于設置控件的內邊距,指定控件內容與其邊界之間的間距。但是需要注意的是,Padding 屬性屬于 Control 類,而不是 FrameworkElement 類。
7. HorizontalAlignment 和 VerticalAlignment 屬性:HorizontalAlignment 屬性和 VerticalAlignment 屬性分別用于設置控件在水平和垂直方向上的對齊方式。它們都是枚舉類型,包括 Left、Center、Right、Stretch 等值。
8. ToolTip 屬性:ToolTip 屬性用于設置控件的工具提示內容,在鼠標懸停在控件上時顯示。
9. Parent 屬性:Parent 屬性用于獲取控件的邏輯父元素,即該控件所在的容器。
10. Style 和 FocusVisualStyle 屬性:Style 屬性用于設置控件的樣式,而 FocusVisualStyle 屬性用于設置控件在獲得焦點時的樣式。
11. Resources 屬性:Resources 屬性用于設置控件本地定義的資源字典,其中包含控件使用的資源。
12. DataContext 屬性:DataContext 屬性用于設置控件的數據上下文,用于實現數據綁定。
13. ContextMenu 屬性:ContextMenu 屬性用于設置控件的上下文菜單。
14. Cursor 屬性:Cursor 屬性用于設置控件在鼠標指針位于其上時顯示的光標形狀。
事件分析:
FrameworkElement 類提供了多個事件,其中比較常用的包括:
1. Initialized 事件:在元素初始化完成后引發。
2. Loaded 事件:在元素被加載到視覺樹中并準備呈現時引發。
3. Unloaded 事件:在元素從視覺樹中移除后引發。
4. SizeChanged 事件:在元素的大小發生變化時引發。
方法成員:
FrameworkElement 類還提供了一些方法成員,比如 FindName、FindResource、TryFindResource、SetBinding 等,用于在代碼中查找元素、資源以及進行數據綁定等操作。
我們來看一下哪些類會繼承這個FrameworkElement基類:
Microsoft.Windows.Themes.BulletChrome
Microsoft.Windows.Themes.ScrollChrome
System.Windows.Controls.AccessText
System.Windows.Controls.AdornedElementPlaceholder
System.Windows.Controls.ContentPresenter
System.Windows.Controls.Control
System.Windows.Controls.Decorator
System.Windows.Controls.Image
System.Windows.Controls.InkCanvas
System.Windows.Controls.ItemsPresenter
System.Windows.Controls.MediaElement
System.Windows.Controls.Page
System.Windows.Controls.Panel
System.Windows.Controls.Primitives.DocumentPageView
System.Windows.Controls.Primitives.GridViewRowPresenterBase
System.Windows.Controls.Primitives.Popup
System.Windows.Controls.Primitives.TickBar
System.Windows.Controls.Primitives.Track
System.Windows.Controls.TextBlock
System.Windows.Controls.ToolBarTray
System.Windows.Controls.Viewport3D
System.Windows.Documents.Adorner
System.Windows.Documents.AdornerLayer
System.Windows.Documents.DocumentReference
System.Windows.Documents.FixedPage
System.Windows.Documents.Glyphs
System.Windows.Documents.PageContent
System.Windows.Interop.HwndHost
System.Windows.Shapes.Shape
代碼示例:
<Window x:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="學習之路" Height="450" Width="800"><Grid><Button x:Name="myButton" Content="點擊一下" Width="100" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center"Click="Button_Click"/></Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;namespace WpfApp2
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){// 設置按鈕的外邊距myButton.Margin = new Thickness(20);// 設置按鈕的背景顏色myButton.Background = Brushes.LightBlue;// 設置按鈕的寬度myButton.Width = 150;// 獲取按鈕的實際寬度double actualWidth = myButton.ActualWidth;// 獲取按鈕的邏輯父元素DependencyObject parent = myButton.Parent;// 查找名為 "myButton" 的元素FrameworkElement? foundElement = this.FindName("myButton") as FrameworkElement;// 注冊名為 "myButton2" 的元素FrameworkElement button2 = new Button();button2.Name = "myButton2";this.RegisterName(button2.Name, button2);}}
}