WPF重要概念
一。邏輯樹、可視樹
邏輯樹:在WPF中,用戶界面里有一個對象樹構(gòu)建而成--邏輯樹
程序里使用的呈現(xiàn)在界面上的對象。
可視樹:有邏輯樹擴展而來。
LogicalTreeHelper.GetChildren(obj as Dependencyobject)
LogicalTreeHelper.FindLogicalNode(obj as Dependencyobject, string name)
VisualTreeHelper.GetChild(obj, i)
二。依賴屬性
1。應(yīng)用:屬性觸發(fā)器、依賴屬性
2。實現(xiàn):與一般屬性使用步驟一致,定義一個變量,定義屬性
代碼
{
//1.定義變量:都必須是Public,Static,并且有一個Property作為后綴
public static readonly DependencyProperty IsDefaultProperty;
/*
//2.創(chuàng)建屬性
typeof(bool) //2.1 依賴屬性類型
typeof(MyButton)//2.2 擁有這個屬性的類
new FrameworkPropertyMetadata(false //2.3 FrameworkPropertyMetadata:可選,界面刷新...
new PropertyChangedCallback(OnIsDefaultChange // 最重要的一個屬性PropertyChangedCallback:屬性值改變時觸發(fā)的事件。
*/
MyButton.IsDefaultProperty = DependencyProperty.Register("IsDefault",
typeof(bool), //2.1 依賴屬性類型
typeof(MyButton), //2.2 擁有這個屬性的類
new FrameworkPropertyMetadata(false, //2.3 FrameworkPropertyMetadata:可選,界面刷新...
new PropertyChangedCallback(OnIsDefaultChanged) // 最重要的一個屬性PropertyChangedCallback:
) // 屬性值改變時觸發(fā)的事件。
);
}
public bool IsDefault //3.使用普通屬性包裝,原則上這里不能添加任何事件,只能是簡單的包裝。
{ //
get { return (bool)GetValue(MyButton.IsDefaultProperty); }
set { SetValue(MyButton.IsDefaultProperty, value); }
}
private static void OnIsDefaultChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
//donginng.....
}
}
3.變更通知應(yīng)用,參考屬性觸發(fā)器(WPF):
代碼
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
4。屬性值繼承:屬性值沿著元素樹的傳遞。
決定作用是否繼承:FrameworkPropertyMetadata.Inherits
5. 依賴屬性取值優(yōu)先級別。
本地值
樣式觸發(fā)器
模版觸發(fā)器
樣式設(shè)置程序
主題樣式觸發(fā)器
主題樣式設(shè)置程序
屬性值繼承.
默認(rèn)值。
6。依賴屬性的事件流程
6.1經(jīng)過<5>取得一個值之后:
代碼
DependencyProperty Register-->ValidateValueCallback; //決定是否可以接受改變。
FrameworkPropertyMetadata -->PropertyChangedCallback; //值改變之后的事件。
6.2 清楚依賴屬性值:實例引用.ClearValue(依賴屬性擁有類名.ForegroundProperty)
6.3. DependencyPropertyHelp.GetValuesource
7.附加屬性
7.1 應(yīng)用:Grid.Row
StackPanel的字體設(shè)置。
7.2 附加屬性:把其他對象上的依賴屬性,添加到本對象上,就像本對象也有這個屬性一樣
7.3 實現(xiàn):最終調(diào)用依賴屬性上的屬性值,依賴屬性值具有繼承關(guān)系。
TextElement.FontSizeProperty = DependencyProperty.RegisterAttached(
"FontSize",typeof(double),
typeof(TextElement),
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,FrameworkPropertyMetadataOptions.Inherits |FrameworkPropertyMetadataOptions.AffectsRender |FrameworkPropertyMetadataOptions.AffectsMeasure),new ValidateValueCallback(xxxx))
)
8 共享屬性。FontSize,
Contral.FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
type(Control),
new FrameworkPropertyMetadata(
SystemFonts.MessageFontSize,
FrameworkPropertyMetadataOptions.Inherits ))
三。路由事件。
1。路由
1。1必要性
1。2 路由類型
Tunneling(管道傳遞,向下)
Bubbling(冒泡,向上)
Direct(直接)不路由,但是可以用于事件觸發(fā)器。
2。實現(xiàn):
2。1
1: public class MyButton : ButtonBase
2: {3: public static readonly RoutedEvent ClickEvent; //1.定¨義?:opublic static
4: 5: static MyButton()
6: {7: //2.生ú成é
8: MyButton.ClickEvent = EventManager.RegisterRoutedEvent(
9: "Click",
10: RoutingStrategy.Bubble,
11: typeof(RoutedEventHandler),
12: typeof(MyButton)
13: );14: }15: //3。£三y包ü裝°可é選?。£
16: public event RoutedEventHandler Click
17: {18: add { AddHandler(MyButton.ClickEvent, value); }
19: remove { RemoveHandler(MyButton.ClickEvent, value); }
20: }21: 22: //4。£觸¥發(fā)¢
23: protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
24: {25: //Base.Do....
26: RaiseEvent(new RoutedEventArgs(MyButton.ClickEvent, this))
27: }28: 29: }30: 2。2參數(shù)介紹RoutedEventArgs
Source-一開始出發(fā)該事件的元素(邏輯樹的第一個元素)
OriginalSource --開始出發(fā)該事件的元素(可視樹的第一個元素)
Handled--True:表示事件已經(jīng)處理,不需要在路由,不管是管道還是冒泡
RoutedEvent --由那個事件觸發(fā)
3。附加事件。
1。定義,每一個路由事件都可以當(dāng)作附加事件使用。
存在必要性
2。實現(xiàn)。
1: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3: x:Class="AboutDialog" ListBox.SelectionChanged="ListBox_SelectionChanged"
4: Button.Click="Button_Click"
5: Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
6: Background="OrangeRed">
7: <StackPanel>
8: <Label FontWeight="Bold" FontSize="20" Foreground="White">
9: WPF Unleashed (Version 3.0)
10: </Label>
11: <Label>? 2006 SAMS Publishing</Label>
12: <Label>Installed Chapters:</Label>
13: <ListBox>
14: <ListBoxItem>Chapter 1</ListBoxItem>
15: <ListBoxItem>Chapter 2</ListBoxItem>
16: </ListBox>
17: <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
18: <Button MinWidth="75" Margin="10">Help</Button>
19: <Button MinWidth="75" Margin="10">OK</Button>
20: </StackPanel>
21: <StatusBar>You have successfully registered this product.</StatusBar>
22: </StackPanel>
23: </Window>
24: 四、如何跟蹤控件一個屬性值的變化
DependencyPropertyDescriptor.FromProperty(控件類型.要跟蹤的某個屬性, typeof(控件類型))
.AddValueChanged(要跟蹤的控件實例, (o, e) =>
{
var i = 1;
});
例如,要跟蹤一個TextBox對象的IsReadOnly屬性值改變,這個對象的實例是textbox:
DependencyPropertyDescriptor.FromProperty(TextBox.IsReadOnlyProperty, typeof(TextBox))
.AddValueChanged(textbox, (o, e) =>
{
var i = 1;
});


浙公網(wǎng)安備 33010602011771號