群共享资源-wpf学习及视频经典教程_第1页
群共享资源-wpf学习及视频经典教程_第2页
群共享资源-wpf学习及视频经典教程_第3页
群共享资源-wpf学习及视频经典教程_第4页
群共享资源-wpf学习及视频经典教程_第5页
免费预览已结束,剩余78页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

WPF体系结 WPF应用程序管 二、创建WPF应用程 WPF窗 StackPanel、WrapPanel、DockPanel容 1、可以使用Orientation属性更改堆叠的顺 一、 二、使用GridSplit分 二、 WPF控件内容模 1、依赖项属性与CLR包装属 WPF命 WPF资 二、资源的定义及XAML中 WPFWPF的基础的体系结构的Assembly如下图所PresentationFramework、PresentationCoremilcoreWPF的主要–core是以非托管代码编写的,目的是实现与DirectX的紧密集成。WPF中的所有显示是通过DirectX引擎完成的,可实现高效的硬件和软件呈现。WPF还要求对内存和执行进行精确控制。milcore中的组合引擎受性能影响关系大,需要放弃CLR的许多优点来提高性能。System.Object类:在.Net中所有类型的System.Windows.Threading.DispatcherObject类:WPFDispatcherObject派生的,这提供了用于处理并发和线程的基本构造。WPF基于调度程序实现 类:表示一个参与依赖项属性系统的对象。System.Windows.UIElement类:UIElement是 WindowsPresentationFoundation(WPF)System.Windows.FrameworkElementWindowsPresentationFoundation(WPF)WPFWPF框架级实现,它是基于由UIElement定义的WPF级API构建的。System.Windows.Controls.Control(UI)元素的基类,这些元素使用ControlTemplate来定义其外观。 类:表示包含单项内容 类:表示一个可用于呈现项的集合System.Windows.Controls.Decorator类:提供在单个子元素(BorderViewbo 类:表示显示图像的控 类:表示包含音频和/ 的控件System.Windows.Controls.PanelPanelPanel元素在WindowsPresentationFoundation(WPF)应用程序中放置和排列子对象。System.Windows.Sharps.SharpEllipse、PolygonRectangle之类的形状WPF应用程序管理1、VisualStudioExpressionBlendApp.xaml文件定义启动应用 文件的内容大致如1:<Application /winfx/2006/xaml/presen /winfx/2006/xaml"4: 6: 7:</Application>StartupUriWPF2、可以自已定义类,定义Main方法实现对WPF1:using2:usingSystem.Windows; 4:namespaceWpfApplicationLifeCycle 5:{ publicclass {staticvoid{//定义Application对Applicationapp=new //方法一:调用Run方法,参数为启动的窗体对象 Window2win=new //Window2win //app.StartupUri=newUri("Window2.xaml",UriKind.R}}}s.ShutdownMode枚举类型,其枚举成员有:OnLastWindowClose(默认值):当应用程序中的最后一个窗体关闭时或调用Application对象的Shutdown()方法时,应用程序关闭;hutdown()方法时,应用程序关闭。(类似于C#的Windows应用程序的关闭模式更改的时候,可以直接在App.xaml中更改1:<Application /winfx/2006/xaml/presen /winfx/2006/xaml"4: 8:也可以在代码8:也可以在代码文件(App.xaml.cs)中更1:Applicationapp=new5:app.ShutdownMode=6: 在异常由应用程序但未进行处理时发生XAML内容之后发生。在调用应用程序中的导航器的StopLoading方法时发Windows会话ApplicationRun在App.xaml文件中:1:<Application /winfx/2006/xaml/presen /winfx/2006/xaml"4: 6: 8:8:App.xaml.cs1:using2: 3:namespaceWpfApplicationLifeCycle4: ///6: ///InteractionlogicforApp.xaml ///</summary> EventArgse) 1:2:staticvoidMain() 3:{ Window2win=new Window2win=new12: 12:15:win.button1.Content=17:win.button1.Content=17:WPF窗VisualStudioExpressionBlend中,自定义的窗体均继承System.Windows.Window类(类型化窗体)。定义的窗体由两部分组成:1: /winfx/2006/xaml"4: 6: Width="300"Height="200">8: <Buttonx:Name="btnMessage"Width="79"Height="24"Cont 14: 1:using2:usingSystem.Windows; 4:namespaceWpfWindow 5:{6: publicpartialclassBasicWindow:Window 8: publicBasicWindow() privatevoidbtnMessage_Click(objectsender,System.Windows.RoutedEventArgse){txtValue.Text= o}}}也可以将代码放在 文件中,上面的例子可以改写为1: /winfx/2006/xaml"4: 6: Width="300"Height="200">8: <Buttonx:Name="btnMessage"Width="79"Height="24"Cont voidbtnMessage_Click(objectsender, txtValue.Text txtValue.Text= o 18: <TextBoxx:Name="txtValue"Width="215"Height="25" Canvas.Left="36"Canvas.Top="48"Text=""TextWrap="20: 21:</Window>构构造方法Show()、ShowDialog()方法Show()方法显示非模态窗口,ShowDialog()方法显示模态窗;LoadedLoaded事件:窗体第一次Show()或ShowDialog()的事件,通常在此事件中Close()Close()方法:关闭窗体,并释放窗体的资 Closing事件、Closed事件:关闭时、关闭后的事件,通常在Closing事件中提示用Activate()Activate()方法Activated、Deactivated事件:当窗体激动、失去焦点时的事 XAML 1:<Window /winfx/2006/xaml/presen /winfx/2006/xaml"4: Title="WindowLifeCycle"Height="200"Width="300" 6: 8: "21" 14: 15:</Window>代码文件:(事件的邦定也在代码中了,这与上面的重复了代码文件:(事件的邦定也在代码中了,这与上面的重复了1:using2:usingSystem.Windows; 4:namespace{///Window1.xaml///publicpartialclassWindow1:{public{this.Loaded+=newRoutedEventHandler(Window_Loaded);this.Activated+=newEventHandler(Window_Activated);this.Closing+=new}privateboolprivatevoidWindow_Loaded(objectsender,RoutedEventArgs{myMedia.SourcenewUri("D:\\没有你的每一天.wma",txtDate.Text=DateTime.Now.ToString("yyyy-MM-isPlaying=}privatevoidWindow_Activated(objectsender,EventArgs{if{isPlaying=true;}}privatevoidWindow_Deactivated(objectsender,EventArgs{if{isPlaying=false;}}privatevoidWindow_Closing(objectsender,ponentModel.CancelEventArgse){stringmessage="Quittheapplication?";stringtitle="SystemInformation";MessageBoxButtonbutton=MessageBoxImageimg=MessageBoxImage.Question;MessageBoxResultresult=MessageBox.Show(message,title,button,if(result=={e.Cancel 消退}}}}WPFWPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与WieMode属性);窗体启动位置(属性)和启动状态(WindowState属性)WindowSt窗体标题(Title属性始终在最前(TopMost属性)是否显示在任务栏1:<Window 10: <PathStroke="DarkGray"StrokeThickness="2"> ="0.8,1" <GradientStopColor="White"Offset="0"></Gra<GradientStopColor="White" 18: 20: <PathFigureStartPoint="40,20"IsClosed="True <ArcSegmentPoint="180,40"Size="20,20"S 37:<LabelWidth="200"Height="120"FontSize="15"HorizontalContentAlignment="Center"VerticalContentAlignment="Center">DragM <LineX1="3"Y1="3"X2="12"Y2="12"<Line<LineX1="12"Y1="3"X2="3"Y2="12"="White" 54:54:1:using2:2:using4:4:namespace public publicpartialclassCustomerWindow: public voidNonRectangularWindow_MouseLeftButtonDown(objectsender,MouseButtonEventArgse) voidcloseButtonRectangle_Click(objectsender,RoutedEventArgse) 23:StackPanel、WrapPanel、DockPanel容一、 是以堆叠的方式显示其中的控1:<StackPanel 8:8:1:<StackPanel 8:8:MarginMargin属性、、、 5:WidthWidth、HeightHorizontalAlignment、 属平对齐为Left、Right或Center,在没有设定宽度的情况下,控件的宽度自动调整MinWidth、MinHeight、MaxWidth、 属二、三、以以上、下、左、右、中为基本结构的布局方式,类似于 AWT布局中BorderLayout控件采用的布局方式为StackPanel方式。1:<DockPanel UniformGrid容器一、GridGrid是以表格形式组织控件的一种布局方式,与JavaAWT中的GridLayout类似,区别在WPF中的Grid的每一个单元格中可以放置多个控但控件可能会层叠在起WPFGrid支持单元格的合并,类HTMLtabletd中的spanGrid中的行和列可以自定义高度(Height)和宽度(Width) Height=”60”:不加“星号”表示固定的高2)Height=”60*”:加“星号”表示”的高度,在调整窗体大小时,高度或宽度会按窗体大小改变的比例进行缩如1: <ButtonGrid.Column="0"Grid.Row="0"Height="30"VerticalA14: <ButtonGrid.Column="0"14:二、使用GridSplit可可以使用GridSplit控件结合Grid控件实现类似于Windows应用程序中SplitContainer的功能,如下面的应用程序:要实现以下的功能:ButtonA和ButtonB、ButtonC组成的整体,可以左右拖动,改变两者的宽度ButtonB和ButtonC可以上下拖动,改变两者的高度实现以上功能的XAML代码如下:1: umn="2" <ButtonContent="ButtonC"Margin="3"Grid.Row="2"Grid.Column="2"/> 21:其其想法为:定义3*3的表格,其中放置分割线的列(下标为1)和行(下标为1)的宽度和高度设置为ButtonA放置在Row=0、Column=0、RowSpan=3ButtonB放置在Row=0、Column=2的单元格中ButtonC放置在Row=2、Column=2的单元格中竖直分割线放置在Row=0、Column=1、ColSpan=3的单元格中水平分割线放置在Row=1、Column=2的单元格中如下图所示:三、UniformGridUniformGrid控件为控件提供了一种简化的网格布局。当控件添加到时,它们会排列在一个网格模式中,该网格模式会自动调整以使控件之间的距离保持均匀。单元格的数目将进行调整,以适应控件的数目。例如如果四个控件添UniformGrid中,它们将安排在包含四个单元格的网格中。如:1: 10: 10: 在使用在使用UniformGrid单元格的数量取决于放入的控件的数量,且单元格一定是行、列数相同的,即1*1、2*2、3*3、4*4…的单元格分布Canvas、 布一、在在WPF中子元素的绝对定位的布局控件其子元素使用Width、Height定义元素的宽度和高度使用Convas.Left(Convas.Right)、Convas.Top(Convas.Bottom)定义与Convas容器的相对位置如果同时存在Convas.Left和Convas.Right 和Bottom,则Convas.Left、Convas.Top优先生效1: <ButtonCanvas.Right="10"Canvas.Bottom="10"Height="23"W6:6:在调整窗体大小时,LT与左、上距离保持不变;RT与右、上距离保持不变;LB序中Acho的功能。二、在在WPF中实现允许使用墨迹的控件。1:<Window /winfx/2006/xaml/presen <ImageWidth="155"Height="155"InkCanvas.Left="10"Ink12:12: 对控件其类型的继承方式如下)ControlTemplate来定义其外的ContentControl是一种包含一段内容ControlButton,ButtonBase,CheckBox,ComboBoxItem,ContentControl,Frame,GridViewColumnHeader,GroupItem,Label,ListBoxItem,ListViewItem,NavigationWindow,RadioButton,RepeatButton,ScrollViewer,StatusBarItem,ToggleButton,ToolTip,UserControl,ContentControl类ContentControl类、HeaderedContentControl类、ItemsControl类HeaderedItemsControl类、Panel类、DecoratorAdornerHeaderedContentControlHeaderedContentControlHeaderedContentControl:包含一段内容并具HeaderHeaderedContentControl是一种包含一段内容HeaderContent,ContentControl类、HeaderedContentControl类、ItemsControl类HeaderedItemsControl类、Panel类、DecoratorAdorner的ItemsControl是一种可包含多个项目(例如字符串、对象或其他元素)ControlItems, Base,Context ,ComboBox,ItemsControl,ListBox,ListView,TabControl,TreeView,Selector,StatusBarContentControl类、HeaderedContentControl类、ItemsControl类HeaderedItemsControl类、Panel类、DecoratorAdornerHeaderedItemsControlHeaderedItemsControl是一ItemsControl,可包含多个项目(例如字符串、对Header,Items, Item对象用 元素的子元素;TreeViewItem对象用TreeView元素的子元素;ToolBar对象用作ToolBarTray的子元素PanelPanel内容模元素WindowsPresentationFoundation(WPF)应用程序中放置和排列子对象种一个或多UIElementPanel内容模型概Canvas,DockPanel,Grid,TabPanel,ToolBarOverflonel,StackPanel,ToolBarPanel,UniformGrid,VirtualizingPanel,VirtualizingStackPanel,WrapPanelPanel类、DecoratorAdornerSystem.Windows.Sharps.Sharp抽象类Ellipse、PolygonRectangle之类的System.Windows.Controls.Decorator类:提供在单个子元素(如BorderViewbDecorator是一FrameworkElement,它将效果应UIElement之上或周围单一Decorator内容模型概ButtonChrome,ClassicBorderDecorator,ListBoxChrome,SystemDropShadowChrome,Border,InkPresenter,BulletDecorator,Viewbox,AdornerDecorator类、Panel类、Decorator类和Adorner类TextBoxTextBoxWPF控件内容模型WPFWPF控件内容模型主要指派生于System.Windows.Controls.Control类的各种控件,WPFContentControl(ContentControl继承的类中的任何类型的对象都可以放置在具有其他三个内容模型中的任何一个的控件1:<Window 6: <ColumnDefinition/>8: <ColumnDefinition/> 10: <RowDefinition/>12: <RowDefinition/> <!--ContentControl示例(Button)<TextBlockText="ContentControl"Grid.Row="0" >18: <StackPanelVerticalAlignment="Center"Horizontaght="48"nter"

<!--HeaderedContentControl示例(GroupBox)<GroupBoxMargin="5,20,5,5"Grid<GroupBoxMargin="5,20,5,5"Grid.Row="0" ght="48" <TextBlockText="ItemsControl"Grid.Row="1"Grid.Colum<ListBoxMargin="5,20,5,5"Grid.Row="1" <TextBlock <TextBlockText="ListItemA" ght="48"/> <TextBlockText="HeaderedItemsControl"Grid.Row="1" TreeNode TreeNode <TextBlockText="NodeA-1" <ButtonContent="NodeA-2" Tree <TextBlockText="NodeB-1" <ButtonContent="NodeB 80:80:ContentControlContentControlContent属性。ContentObject,序标记语言(XAML)或代码来设置Content。ContentControlButton、But 、、boBoxItem、ContentControl、Frame、GridViewColumnHeader、GroupItem、Label、ListBoxItem、ListViewItem、NavigationWindow、RadioButton、RepeatButton、ScrollViewer、StatusBarItem、Toggle在Content中只能放置一个控件(可以放置一个容器,然后再在容器中放置多个控件)以省略此标记。如在按钮中放置一可以有以下几种写法:2:2:<Button 6:6:9:<Button 11:13:<!--如果是字符串,或者是数组绑定、资源还可以--另外,还可以使用代码来为ContentControl指定相应的Content属性1:TextBlockdate=newTextBlock();8:4:TextBlocktime=new8:4:TextBlocktime=new11:btn.Content=panel;HeaderedContentControlHeaderedContentControl类继承ContentControl类,表示带有Header的ContentControl,其除了具ContentControlContent属性外,还具有一Header属性,Header的类型也是Object对象,与Content属性的用法类似。HeaderedContentControl继承的控件有:Expander、GroupBox、TabItem如定义一个带有和文字标题的 /winfx/2006/xaml/presen /winfx/2006/xaml"4: 6: <ExpanderMargin="5"HorizontalAlignment="Center"> eight="32"/> <TextBlockText="Male"HorizontalAlignment="L <TextBlockText="23"HorizontalAlignment="Lef 41:ItemsControlItemsControlListBox。可以使用ItemsSource属性或Items属性来填充一个ItemsControl。在每一项中显示字体的样式(类似于Office系列中的字体下拉菜单):1:<Window /winfx/2006/xaml/presen /winfx/2006/xaml"4: Title="ItemsControl"Height="300"Width="300"> 6: <ListBoxx:Name="lstFont"/> 8:8:1:using2:usingSystem.Windows; 3:usingSystem.Windows.Controls;4:usingSystem.Windows.Data; 5:usingSystem.Windows.Markup;6:usingSystem.Windows.Media; 8:namespaceWPFControlContentModule 9:{10: ///<summary> ///InteractionlogicforwinItemsControl.xaml12: ///</summary> publicpartialclasswinItemsControl:Window{public{Bindingbinding=newbinding.Source=Items;ty, privateList<TextBlock> List<TextBlock>result=newList<TextB foreach(FontFamilyfamilyin irin TextBlockt=newt.Text=t.FontFamily=t.FontSize=}} 55:WPFItemsControlItemsControlWPFItemsControl2:2:ItemB"ItemItem <TextBlockText="ItemB" HeaderedItemsControlHeaderedItemsControlItemsControl类继承。HeaderedItemsControlHeaderHeaderedContentControl.WPFHeader属性附带三个从HeaderedItemsControl继承的控件:Item、ToolBar、TreeVi和TreeViewItem为例:1: <TextBlockText="A-1" <TextBlockText="A-1" <TextBlockText="A-2" <TextBlock <TextBlockText="A-2-1" <TextBlockText="A-2-2" <TextBlockText="A-2-3" <TextBlockText="RootNodeB"/> <TextBlock <TextBlockText="B-1" <TextBlockText="B-1-1"-1--1--2"-2--2--2- 容器。Panel内容模型包含的容器有: 对于Panel模型,其包含一个Children属性,表示其所有的子控件 容器的集合,在XA 1:<StackPanel 10:10:也可以通过代码,动态添加Children中的对2:2:Buttonbtn=new3:btn.Content="Button6:6:要是对其中的一个子元素的边缘进行修饰。Decorator模型的主要控件包含:Decorator模型包含一个Child属性,表示其包含的一个子元素(注意,只能是一个子元素(控件或容器,在容器中可以再添加其他的控件)),Child属性的 标记可以省略例如,对于一个TextBox添加一个边框,使用XAML语言定1:<StackPanel 7:4:border.BorderBrush=new4:border.BorderBrush=new2:2:Borderborder=new5:border.Margin=newThickness(5);8:8:TextBoxtextBox=new9:textBox.Text="TextBoxContent12:12:border.Child=15:TextBlockTextBlock模型实际上指的就是System.Windows.Controls.TextBlock类,它是一个用于显示少量流内容的轻量控件。其中包含InLines属性,支持Inline流内容元素的承载和显示AnchoredBlock、Bold(粗体字符串)、Hyperlink(超,在浏览器支持的模式下有效)、InlineUIContainer(承载其他控件的容器)、I颜色等的Span)和Underline(下划线)。1:<StackPanel <HyperlinkNavigateUri=" <Span <Run <Run "5" 44:44:TextBlockmyTextBlock=newTextBlock();ItalicmyItalicnewItalic(newRun("(如WidthHight等)"));myItalic.FontSize=24;myItalic.Foreground=Brushes.Purple;myTextBlock.TextWrap=TextWrapBoldmyBold=newBold(newItalic(newRun("(Device-independent

myTextBlock.HorizontalAlignment=HorizontalAlignment.Stretch;myTextBlock.Inlines.Add(newLineBreak());BoldmyBold1newBold(newRun("粗体"));UnderlinemyUnderlinenewUnderline(newRun("下划线"));myTextBlock.Inlines.Add(newLineBreak());HyperlinkmyHyperlink=newHyperlink(newRun(""));myHyperlink.NavigateUri=newUri("ht myTextBlock.Inlines.Add(newLineBreak());SpanmySpan=newSpan(newRun("Span设置字体、颜色等"));mySpan.ForegroundBrushes.Red;//或者改为:mySpan.Foreground=newInlineUIContainermyInlineUIContainer=newInlineUIContainer();StackPanelmyStackPanel=newStackPanel();myInlineUIContainer.Child=myStackPanel;ButtonmyButton=newButton();myButton.Content="lucky";((代码部分的执行结果)System.Windows.Controls.TextBoxSystem.Windows.Controls.TextBox类,实现的是可编辑的文本框,文本框的内容由字符串类型的Text属性指定,并且整个TextBox的内容使用共同的(即TextBox指定的依赖项属性和路由事件一、依赖项属性(Dependency一、依赖项属性(DependencyWindowsPresentationFoundation(WPF)提供了一组服务,这些服务可用于扩展公共(CLR)WPFWPF属性系统WPF(XAML) 以以Button的Backgroud为例,设置或获取其值可以有以下几XAML文件1: 在所生成的代码中,XAMLXAML WPF类型(ColorSolidColorBrush) <ButtonMargin="3"Content="ButtonB"x:Name="btn_ButtonB 22:22:12:2:btn_ButtonC.Background=new6:6:10:10:SolidColorBrushb_Brush1=(SolidColorBrush)(btn_ButtonB.Back11txt_Value1.Textb_Brush1.Color.ToString();16:txt_Value2.Text=16:txt_Value2.Text=开发人员工具公开属性)。但是在某些情况下适合直接调用API。14:SolidColorBrushb_Brush2=(SolidColorBrush)(btn_ButtonB.GetV都表示或支持整套WPF功能中的特定功能:WPFcyObject类,依赖项属性是通过调用Register方法(或RegisterReadOnly,自定义的只读的依赖项属性在WPF属性系统中 ,并通过DependencyProperty标识符字段备份的属性。依赖项属性只能由DependencyObject类型使用,但DependencyObject在WPF类层次结构中的级别很高,因此,WPF中的大多数可用类都支持依赖项属性。在对依赖项属性及CLR包装属性命名时必须满足:CLR包装属性名+Property=依赖项属性例如:在某DependencyObject类的子类1://定义并依赖项属2:2:publicstaticreadonlyDependencyPropertyAquariumGraphicProperty= // newFrameworkPropertyMetadata(//new)16:15:publicUri16: 19:二、路由事件1:een" 14:14:1:privatevoidbtn_B_Click(objectsender,RoutedEventArgse) "Click "ClickBluePanelButton2: ButtonsourceButton=(Button)string.Format("SourceButtonis{0}!", "System 18:18: "System "ClickGreenPanelButton!", string.Format("senderTypeis{0}.",sender.GetType().N string.Format("e.Source string.Format("e.SourceTypeis{0}.",e.Source.GetType ButtonsourceButton= string.Format("SourceButtonis{0}!", "System 40:40:43: "System "ClickRed "System 49:52:52: "Click "ClickRedPanelButtonA "System 58:58:61: "System "ClickRedPanelButton "System e.Handled=69: sender指的就是由哪个对 的事类型及其子类型的事件,统一绑定到一个事件处理方法上,统一处理。此件响应方法中senderpanel_Greene.Source指的是ButtonBase.Click的某=键盘输入、鼠标输入、焦点处理件、方法和属性,这些事件、方法和属性提供有关键盘状态的信息。Keyboard的事件也通过UIElement等XAML基元素类的事件向外提供。KeyUpPreviewKeyUp事件:处理键盘键抬起;KeyDownKeyUp事件属于冒泡路由事件,而PreviewKeyDown 属于隧道路由事trueStackPanelCanvasPanelFocusable的默认值设置1:<Window /winfx/2006/xaml" Title="Window1"Height="300" Focusable="True" <!--...-- 9:1:privatevoidWindow_PreviewKeyDown(objectsender,2:2: if(e.Key== 按下“静音” txtMessage.Text= e.Handled= elseif(e.Key== 按下“增大音量” txtMessage.Text= e.Handled= elseif(e.Key== 按下“减小音量” txtMessage.Text= e.Handled= 21:WPFWPF提供的System.Input.Mouse类提供与鼠标相关的事件、方法和属性,这些事件、方法和属性提供有关鼠标状态的信息。与Keyboard类类似,其事件也通过UIElement等基其事件主要有以下(每个事件均XXX冒泡路由事件和PreviewXXX隧道路由事件MouseDownMouseDown、MouseUpMouseWheel:处理鼠标滚轮滚动;另外,对于鼠标位置的捕获,使用Mouse类的GetPositionUIElement1:<RectangleCanvas.Left="246"Canvas.Top="46" Name="mainRectangle"Stroke="Black" 1:privatevoidmainRectangle_MouseEnter(objectMouseEventArgs{mainRectangle.Fill=new}7:privatevoidmainRectangle_MouseLeave(objectMouseEventArgs{mainRectangle.Fill=new}14:13:privatevoidmainRectangle_MouseMove(object14: PointpointBaseRectangletxtMessage.Text="MousePosition(BasetheRectangle)istxtMessage.Text+=PointpointBaseWindow=txtMessage.Text+="MousePosition(BasetheWindow)ispointBaseWindow.X,}30:30:privatevoidmainRectangle_MouseDown(object{MouseButtonbutton=txtMessage.Text+=txtMessage.Text+="MouseButtonis{0}",38:40:40:privatevoidmainRectangle_MouseWheel(object41: if(e.Delta> if(e.Delta< 53:元素。在WPF中,具有键盘焦点的元素会将IsKeyboardFocused设置为true。Keyboard类的静态属性FocusedElement获取当前具有键盘焦点的元素。FocusableIsVisibletrue。有些类(Panel基类)Focusable设置为falseFocusabletrue可以通过用户与可以通过用户与UI交互(例如,按Tab键定位到某个元素或者在某些元素上单击鼠标)KeyboardFocus点。 方法尝试将键盘焦点给予指定的元素。返回的元素是具有键盘焦点的元素,FocusManager..::.FocusedElement踪其范围内的FocusedElement的元素。当键盘焦点离开焦点范围时,焦点元素会失去个具有逻辑焦点的元素。GetFocusScope返回指定元素的焦点范围。WPFWindowItem、ToolBar。GetFocusedElement获取指定焦点范围的焦点元素。SetFocusedElement点范围中的焦点元素。SetFocusedElement当按下导航键之一时,当按下导航键之一时,KeyboardNavigation键。可以通过设置附加的KeyboardNavigation属性TabNavigation、ControlTabNavigationDirectionalNavigation来更改导航容器的导航行为。这些属OnceNoneContinue与键盘焦点相关的事件有与键盘焦点相关的事件有PreviewGotKeyboardFocus、GotKeyboardFocus、PreviewLostKeyboardFocusLostKeyboardFocusKeyboard会PreviewGotKeyboardFocusPreviewLostKeyboardFocusEventHandledtrueWPF命WPFWPF系统(WPF)1: <RowDefinitionHeight="23" <RowDefinition Grid.Row="0" Item Item Copy" Item Cut" Item Paste" <TextBoxGrid.Row="1"Grid.Column="0" ="Wrap"AcceptsReturn="True"18:18:ItemmainTextTextBox“一、命令WPFWPFICommand接口来创建的。ICommand(ExecuteCanExecute)(CanExecuteChanged)。Execute操 行。mandWPFmandmand上的ExecutePreviewExecutedExecutedmandCanExecuteCanExecutePreviewCanExecuteCommandBindingWPF、、mands WPF已封装令类有命令示例命令Close、Cut、Copy、Paste、Save、BrowseForward、BrowseBack、Zoom、AlignXXX、MoveXXX、Play、Pause、NextTrack、IncreaseVolume、Record、MoveXXX、SelectXXX、ScrollXXX、XXXXXXMoveNextMovePreviousmandsmandsItem、ButtonKeyGestureWPFICommandSourceICommandSource、mandTargetCommandCommandTargetWPFICommandSourceCommandTargetICommandmandICommandSourceCommandParameterICommandSourceWPF类包括:ButtonBaseItem、HyperlinkItemHyperlinkInputBindingInputGestureButtonBaseCommand1:<Button mands.Copy"而而InputBinding使用KeyBinding或MouseBinding绑定特定的输入手势到某一命令例如在WindowCtrl+F2快捷键mands.Open1:<KeyBinding Key="F2"Modifiers="Control"mandExecutedCanExecuteWPFICommandSourceCommandTarget属性只有ICommandmandICommandSource而对ItemTextBoxPasswordBox件的应用程序中调用“粘贴”命令,则目标既可以是TextBox,也可以是PasswordBox,具Paste命令mainText控件1: Item Paste" CommandBindingCommandBindingCommandBindingCommandPreviewExecuted、Executed、PreviewCanExecute和CanExecute事件。CommandCommandBindingExecutedPreviewCanExecuteCanExecuteCanExecutePreviewCanExecuteEventArgsButtonItemCanExecuteFalse时,会“ExecutedPreviewExecuted1:<Windowm mandDemo"Height="300" <KeyBinding Key="F3"Modifiers="Control" <CommandBinding <RowDefinitionHeight="23" <RowDefinition Grid.Row="0" Item Item Save" <TextBoxGrid.Row="1"Grid.Column="0" ="Wrap"AcceptsReturn="True" 28:28:1:privatevoidCommandBinding_Save_CanExecute(objectsender,CanExecuteRoutedEventArgse)2: if(mainText.Text==2: e.CanExecute= e.CanExecute= 12:12:14:14:privatevoidCommandBinding_Save_Executed(object15: //保存文 SaveFileDialogsave=newsave.Filter文本文件|*.txt|所有文件bool?result= if{ }25:WPF资WPF资源作为重用通常定义的对象和值的简单方法。例如定义一种可以复用的单色的Brush对象,按钮的背景及矩形的填充颜色均使用此Brush:1:<Window /winfx/2006/xaml" Title="BasicResource"Height="200" <SolidColorBrushx:Key="myBrush"Color="Gold" <ButtonMargin="5"Content="SampleButton"Background="{StaticResourcemyBrush}"/><Rectangle<RectangleMargin="5"Width="100"Fill="{StaticResourcemyBrush}"12:12: 中资源通常用作“样式”(Style)、样式模板、数据模板等1:<Application /winfx/2006/xaml" <SolidColorBrushColor="Gold"x:Key="myGoldBrush" 8:8:在ApplicationResourceDemo.xaml文件(窗体)中使用App.xaml中定义的1:<Window /winfx/2006/xaml" <ButtonMargin="5" 8:8:定定义在Window或Page中,作为一个窗体或页面共享的资源1:<Window /winfx/2006/xaml" Title="WindowResourceDemo"Height="300" <SolidColorBrushx:Key="myRedBrush"Color="Red" <ButtonMargin="5"Background="{StaticResourcemyRedBrush}">SampleButton</Button> VisualStudioWPF1: /winfx/2006/xaml/presentation" <SolidColorBrushx:Key="myWhiteBrush"Color="White"4:4:在 文件(窗体)中,将为窗体级的资1:<Window /winfx/2006/xaml" Title="FileResourceDemo"Height="300" <ResourceDictionary <ButtonMargin="5"Background="{StaticResourcemyWhiteBrush}">SampleButton</Button> 一个资源,供Button内的Content控件使用1:<Window /winfx/2006/xaml" <Button Color="Green"<SolidColorBrush <TextBlock 15:XAMLx:KeyApp.xaml文件1:<Application /winfx/2006/xaml" <SolidColorBrushColor="G

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论