您的位置:首页 > 其它

WPF重要概念

2010-06-02 20:37 141 查看
一。逻辑树、可视树

逻辑树:在WPF中,用户界面里有一个对象树构建而成--逻辑树

程序里使用的呈现在界面上的对象。

可视树:有逻辑树扩展而来。

LogicalTreeHelper.GetChildren(objasDependencyobject)
LogicalTreeHelper.FindLogicalNode(objasDependencyobject,stringname)
VisualTreeHelper.GetChild(obj,i)
二。依赖属性

1。应用:属性触发器、依赖属性

2。实现:与一般属性使用步骤一致,定义一个变量,定义属性

.src_container{background-color:#e7e5dc;width:99%;overflow:hidden;margin:12px012px0!important;padding:0px3px3px0px}
.src_container.titlebar{background-color:#d4dfff;border:1pxsolid#4f81bd;border-bottom:0;padding:3px24px;margin:0;width:auto;line-height:120%;overflow:hidden;text-align:left;font-size:12px}
.src_container.toolbar{display:inline;font-weight:normal;font-size:100%;float:right;cursor:hand;color:#00f;text-align:left;overflow:hidden}
.toolbarspan.button{display:inline;font-weight:normal;font-size:100%;cursor:hand;color:#00f;text-align:left;overflow:hidden;cursor:pointer;}
.src_containerdiv.clientarea{background-color:white;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;height:auto;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys",courier,monospace,serif}
.src_containerol.mainarea{padding:00052px;margin:0;background-color:#f7f7ff!important}
.number_show{padding-left:52px!important;list-style:decimaloutside!important}
.number_showli{list-style:decimaloutside!important;border-left:1pxdotted#4f81bd}
.number_hide{padding-left:0px!important;list-style-type:none!important}
.number_hideli{list-style-type:none!important;border-left:0px}
ol.mainareali{display:list-item!important;font-size:12px!important;margin:0!important;line-height:18px!important;padding:0000px!important;background-color:#f7f7ff!important;color:#4f81bd}
ol.mainarealipre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrapol.mainarealipre{white-space:pre-wrap;white-space:-moz-pre-wrapwhite-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}
ol.mainarealipre.alt{background-color:#f7f7ff!important}

functionCopyCode(key){varcodeElement=null;vartrElements=document.all.tags("ol");vari;for(i=0;i代码

FrameworkPropertyMetadata-->CoerceValueCallback;//可以强制改变传递的值。
DependencyPropertyRegister-->ValidateValueCallback;//决定是否可以接受改变。
FrameworkPropertyMetadata-->PropertyChangedCallback;//值改变之后的事件。
6.2清楚依赖属性值:实例引用.ClearValue(依赖属性拥有类名.ForegroundProperty)
6.3.DependencyPropertyHelp.GetValuesource

7.附加属性

7.1应用:Grid.Row

StackPanel的字体设置。

7.2附加属性:把其他对象上的依赖属性,添加到本对象上,就像本对象也有这个属性一样

7.3实现:最终调用依赖属性上的属性值,依赖属性值具有继承关系。

隐藏行号复制代码?这是一段程序代码。

TextElement.FontSizeProperty=DependencyProperty.RegisterAttached(

"FontSize",

typeof(double),

typeof(TextElement),

newFrameworkPropertyMetadata(

SystemFonts.MessageFontSize,

FrameworkPropertyMetadataOptions.Inherits|

FrameworkPropertyMetadataOptions.AffectsRender|

FrameworkPropertyMetadataOptions.AffectsMeasure),

newValidateValueCallback(xxxx)

)

)



.src_container{background-color:#e7e5dc;width:99%;overflow:hidden;margin:12px012px0!important;padding:0px3px3px0px}
.src_container.titlebar{background-color:#d4dfff;border:1pxsolid#4f81bd;border-bottom:0;padding:3px24px;margin:0;width:auto;line-height:120%;overflow:hidden;text-align:left;font-size:12px}
.src_container.toolbar{display:inline;font-weight:normal;font-size:100%;float:right;cursor:hand;color:#00f;text-align:left;overflow:hidden}
.toolbarspan.button{display:inline;font-weight:normal;font-size:100%;cursor:hand;color:#00f;text-align:left;overflow:hidden;cursor:pointer;}
.src_containerdiv.clientarea{background-color:white;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;height:auto;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys",courier,monospace,serif}
.src_containerol.mainarea{padding:00052px;margin:0;background-color:#f7f7ff!important}
.number_show{padding-left:52px!important;list-style:decimaloutside!important}
.number_showli{list-style:decimaloutside!important;border-left:1pxdotted#4f81bd}
.number_hide{padding-left:0px!important;list-style-type:none!important}
.number_hideli{list-style-type:none!important;border-left:0px}
ol.mainareali{display:list-item!important;font-size:12px!important;margin:0!important;line-height:18px!important;padding:0000px!important;background-color:#f7f7ff!important;color:#4f81bd}
ol.mainarealipre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrapol.mainarealipre{white-space:pre-wrap;white-space:-moz-pre-wrapwhite-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}
ol.mainarealipre.alt{background-color:#f7f7ff!important}

functionCopyCode(key){varcodeElement=null;vartrElements=document.all.tags("ol");vari;for(i=0;i8共享属性。FontSize,

Contral.FontSizeProperty=TextElement.FontSizeProperty.AddOwner(

type(Control),

newFrameworkPropertyMetadata(
SystemFonts.MessageFontSize,

FrameworkPropertyMetadataOptions.Inherits))
三。路由事件。

1。路由

1。1必要性

1。2路由类型

Tunneling(管道传递,向下)

Bubbling(冒泡,向上)

Direct(直接)不路由,但是可以用于事件触发器。

2。实现:

2。1

publicclassMyButton:ButtonBase
{
publicstaticreadonlyRoutedEventClickEvent;//1.定¨义?:opublicstatic
staticMyButton()
{
//2.生ú成é
MyButton.ClickEvent=EventManager.RegisterRoutedEvent(
"Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MyButton)
);
}
//3。£三y包ü装°可é选?。£
publiceventRoutedEventHandlerClick
{
add{AddHandler(MyButton.ClickEvent,value);}
remove{RemoveHandler(MyButton.ClickEvent,value);}
}
//4。£触¥发¢
protectedoverridevoidOnMouseLeftButtonDown(MouseButtonEventArgse)
{
//Base.Do....
RaiseEvent(newRoutedEventArgs(MyButton.ClickEvent,this))
}
}


.codearea{color:black;background-color:white;line-height:18px;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys","BitStreamVeraSansMono",courier,monospace,serif}
.codeareapre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrappre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;word-break:normal}
.codeareapre.alt{background-color:#f7f7ff!important}
.codearea.lnum{color:#4f81bd;line-height:18px}

2。2参数介绍RoutedEventArgs

Source-一开始出发该事件的元素(逻辑树的第一个元素)

OriginalSource--开始出发该事件的元素(可视树的第一个元素)

Handled--True:表示事件已经处理,不需要在路由,不管是管道还是冒泡

RoutedEvent--由那个事件触发

3。附加事件。

1。定义,每一个路由事件都可以当作附加事件使用。

存在必要性

2。实现。

<Windowxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AboutDialog"ListBox.SelectionChanged="ListBox_SelectionChanged"
Button.Click="Button_Click"
Title="AboutWPFUnleashed"SizeToContent="WidthAndHeight"
Background="OrangeRed">
<StackPanel>
<LabelFontWeight="Bold"FontSize="20"Foreground="White">
WPFUnleashed(Version3.0)
</Label>
<Label>©2006SAMSPublishing</Label>
<Label>InstalledChapters:</Label>
<ListBox>
<ListBoxItem>Chapter1</ListBoxItem>
<ListBoxItem>Chapter2</ListBoxItem>
</ListBox>
<StackPanelOrientation="Horizontal"HorizontalAlignment="Center">
<ButtonMinWidth="75"Margin="10">Help</Button>
<ButtonMinWidth="75"Margin="10">OK</Button>
</StackPanel>
<StatusBar>Youhavesuccessfullyregisteredthisproduct.</StatusBar>
</StackPanel>
</Window>


.codearea{color:black;background-color:white;line-height:18px;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys","BitStreamVeraSansMono",courier,monospace,serif}
.codeareapre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrappre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;word-break:normal}
.codeareapre.alt{background-color:#f7f7ff!important}
.codearea.lnum{color:#4f81bd;line-height:18px}

四、如何跟踪控件一个属性值的变化

DependencyPropertyDescriptor.FromProperty(控件类型.要跟踪的某个属性,typeof(控件类型)).AddValueChanged(要跟踪的控件实例,(o,e)=>
{
vari=1;
});

例如,要跟踪一个TextBox对象的IsReadOnly属性值改变,这个对象的实例是textbox:

DependencyPropertyDescriptor.FromProperty(TextBox.IsReadOnlyProperty,typeof(TextBox)).AddValueChanged(textbox,(o,e)=>
{
vari=1;
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: