WPF Quick Mastery Edition

Source: Internet
Author: User
Tags define local mscorlib xpath

Namespaces: xmlns:sys= "Clr-namespace:system;assembly=mscorlib" xmlns:local= "clr-namespace:usernamespace" WPF layout 1.Cavas Canvas : Set the coordinate values for the layout, top, left, Bottom, and right such as: Canvas.settop (control name, numeric value); 2.DockPanel: Provides a relative docking position for its child controls, including the dock to starboard (dock.left) (dock.right), dock Down (dock.bottom), dock up (dock.up) 3.StackPanel face: Arranges controls in the direction of horizontal or vertical lines, in the direction specified (HorizontalAlignment, VerticalAlignment) to add a child control to it. 4.WrapPanel: The layout of the wrapped child control, when a child row (or column) is not enough to place the new control, WrapPanel automatically places the child control on the new row (or new column, This depends on the Orientation property) 5.Grid grid: The grid is laid out by a custom row and column, which is similar to a table. 6.UniGrid average grid: Grids are laid out by customizing the rows and columns, with each grid size. 7.ViewBox View: You can automatically scale 8. ScrollViewer flow view WPF Resources 1. Resources are typically used as style, style templates, data templates, and so on 1). Application-level resources: defined in the App.xaml file as a resource shared by the entire application <Application.Resources> <solidcolorbrush color= "Red" x:key= "Redbrush "></SolidColorBrush> </Application.Resources> <button background=" {StaticResource resourcekey= Redbrush} "> Use Application Set Resource </button>2). Form resource: defined in Window or page, the existence of a resource that is shared as a form or a sheet <Window.Resources> <solidcolorbrush color= "Red" x:key= "REDBRUsh "></SolidColorBrush> </window.resources><button background=" {StaticResource resourcekey= Redbrush} "> Use app Form Resources </Button> 3). File resource: Defined in a XAML file in a resource dictionary <Window.Resources> <resourcedictionary source= "Skin1.xaml" ></resourcedict Ionary> </window.resources>button background= "{StaticResource Resourcekey=redbrush}" > Working with File Resources </ Button> 4). Object (Control) resource: defined in a ContentControl as a resource shared by its child containers, child controls <stackpanel x:name= StackPanel > <stackpanel x : Name= "StackPanel" > <StackPanel.Resources> <resourcedictionary source= "Skin1.xaml" &GT;&L t;/resourcedictionary> </StackPanel.Resources> <button background= "{StaticResource Resourcekey=redbru SH} "> Using app Control Resources </Button> </StackPanel> 2. The order in which the resource file is parsed is similar to cascading style sheets, where the precedence is from high to the bottom: Object-level, form-level, application-set. Static resources (StaticResource) and dynamic Resources (DynamicResource) 3. Static resources (StaticResource) and dynamic resource (DynamicResource) resources can be referenced as static or dynamic resources. This is done by using the StaticResource markup extension or DynamicresouRCE markup extension is complete. Typically, resources that do not need to be changed at run time use static resources, and resources that need to be changed at run time use dynamic resources. Dynamic resources need to use more overhead than static resources.     4. Use mscorlib resources 1). window to add the namespace xmlns:sys= "Clr-namespace:system;assembly=mscorlib" 2). Add Resources <Window.Resources>     <sys:string x:key= "myString" > String!!!!! </sys:string><x:array type= "sys:string" x:key= "MyArray" ><sys:String> data 1</sys:string>< sys:string> data 2</sys:string></x:array></window.resources>3). Use resource <textblock x:Name= "Txt2" text= "{Binding source={staticresource resourcekey=mystring}}"/>4). The background invoke resource string str=this. FindResource ("myString") as string;local Resource 1). Add a spatial reference xmlns:local= "Clr-namespace:usernamespace" 2). Define a daemon public class PATHCONVERTER:IVALUECONVERTER{}3). Define local resources <window.resources><local:pathconverter x:Key= "PC" ></ LOCAL:PATHCONVERTER&GT;&LT;/WINDOW.RESOURCES&GT;4). Define the local resource <image source= "{Binding Name, converter={ StaticResource pc}} "/>5. Extensible tagging resource 1). Define Resources <Window.Resources> <solidcolorbrush X:shared= "false" x:key= "brush" color= "red" ></SolidColorBrush> </window.resources>2). Call Tag <button content= "button" background= "{StaticResource Resourcekey=brush}"/>3). Background programming private void button1_click (object            sender, RoutedEventArgs e) {SolidColorBrush SCB = this.button2.Background as SolidColorBrush; Scb. Color=colors.green;}        The style in WPF "Can only change an existing property value of a control" style is used as a resource <window.resources> <style targettype= "button" x:key= "Keystyle" > <setter property= "Foreground" value= "Blue"/> ... </style> <style targettype= "button" basedon=        "{StaticResource Keystyle}" > <Style.Triggers> <trigger property= "ispressed" value= "True" >> </Trigger> </Style.Triggers> </style></window.resources>targettype= "control type" indicates the Style's The Action object is the control type setter set property= "property" value= "property value" x:key= "key unique name" call method: Style= "{StaticResource key unique name}" basedon= "{ StaticResource inherits another style of key} "background programming tHis. Button1.style = (Style) findresource ("Keystyle"); WPF references an external style implementation method: 1. Create a new item Add/resource dictionary style.xaml, and add a style style <resourcedictionary xmlns= "http://schemas.microsoft.com/ Winfx/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "> <s Tyle x:key= "BaseStyle" targettype= "{x:type Control}" > <setter property= "Margin" value= "5"/> <     /style> <style targettype= "{x:type button}" basedon= "{StaticResource BaseStyle}" > </Style> <style targettype= "{x:type TextBox}" basedon= "{StaticResource BaseStyle}" > </Style> ... </resour Cedictionary>2. Referencing external resources, note: Adding an external style to a window requires you to specify a key, while the application does not require 1). Add external styles to window <Window.Resources> <resourcedictionary x:key= "Rdstyle" > <resourced Ictionary. mergeddictionaries> <resourcedictionary source= "Style.xaml"/> </resourcedicti Onary. Mergeddictionaries> </ResourceDictionary> </Window.Resources> <Application.Resources> <resourcedictionar y> <ResourceDictionary.MergedDictionaries> <resourcedictionary source= "Style.xaml"/ > </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </application.reso Urces>3. Applying styles to the layout of a window <grid resources= "{StaticResource rdstyle}" > ... </grid>wpf referencing styles in other assemblies <wind ow. Resources > <ResourceDictionary> <ResourceDictionary.MergedDictionaries> & Lt ResourceDictionary source= "pack://application:,,,/resourcelibrary;        Component/themes/styles.xaml "></ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </window.resources>style Trigger (Trigger) "is also a style, trigger is a preset style" 1). Property trigger Trigger, When consistent with the preset property values, the style of the preset is triggered <Window.Resources> <style TargettYpe= "button" > <Style.Triggers> <trigger property= "ispressed" value= "True" > <setter Property= "Foreground" value= "Red"/> </Trigger> </Style.Triggers> </Style> </w Indow. Resources> Note: <trigger property= "ispressed" value= "true", when the property ispressed is true, the Trigger preset style is triggered; 2). Multi-Attribute trigger multitrigger<multitrigger> <MultiTrigger.Conditions> <condition property= "Property One" value= "value One" > </Condition> <condition property= "attribute two" value= "value two" ></Condition> </MultiTrigger.Conditions> </MultiTrigger> <MultiTrigger.Setters>//style setter</multitrigger.setters> 3). Data Trigger DataTrigger <Style.Triggers> <datatrigger binding= "{Binding path=name}" value= "Yang" > <setter property= "Foreground"  Value= "Red" ></Setter> </DataTrigger> </style.triggers>xaml page <textbox text= "{Binding Path=name} "></TextBox> 4. Multi-conditional data trigger MultidatatriggermultiDataTrigger This is similar to MultiTrigger5. Event Trigger Eventtrigger<style.triggers><eventtrigger routedevent= "MouseEnter" >//style ControlTemplate (Control template) in Setter</eventtrigger> &LT;/STYLE.TRIGGERS&GT;WPF makes a new control by combining multiple controls WPF contains data templates and control templates, where control templates include ControlTemplate and itemspaneltemplate in fact, every control in WPF has a default template that describes how the control looks and how it reacts to external stimuli. We can customize a template to replace the control's default template to create a personalized control. ControlTemplate contains two important attributes: 1,visualtree, the visual tree of the template, in fact we use this property to describe the appearance of the control 2,triggers, the list of triggers, which contains a number of trigger trigger, We can customize this trigger list to make the control react to stimuli in the outside world, such as when the text becomes bold when the mouse passes. Example: Drawing a circle <button content= control Contents > <Button.Template> <ControlTemplate> <!--defining the visual tree _start--> <Grid> <ellipse width= "{TemplateBinding button.width}" height= "{Templatebind ing control.height} "fill=" {TemplateBinding button.background} "/> <textblock name=" Txtblock "margin=" { TemplateBinding button.padding} "verticalalignment=" center "horizontalalignment=" center "text=" {TemplateBinding Button.content} "/>              </Grid> <!--Define a visual tree _end--> <!--define triggers---<ControlTemplate.Triggers> <trigger property= "Button.ismouseover" value= "True" > <setter property= "Button.foreg            Round "value=" Red "/> </Trigger> </ControlTemplate.Triggers> <!--define triggers _end--> </ControlTemplate> </Button.Template> </Button> Comments: width= "{TemplateBinding button.width}", the Width property of the bound control <button.template>, template definition <controltemplate>, control template definition, Inside is the trigger 2 that defines the visual tree that is the typesetting event trigger (EventTrigger) that triggers the preset event when it is invoked. Templates can be set independently <Window.Resources> <controltemplate targettype= "Button" x:key= "Buttontemplate" > <!--Define the visual tree--<Grid> <ellipse width= "{Templatebin Ding Button.width} "height=" {TemplateBinding control.height} "fill=" {TemplateBinding button.background} "/> &L T TextBlock margin= "{Templatebinding button.padding} "verticalalignment=" center "horizontalalignment=" center "text=" {TemplateBinding button.content} " /> </Grid> <!--define a visual tree _end--><!--Define a control resource--><controltemplate.resources></controlte Mplate. resources><!--Defining Control Resources _end--> < defining triggers!----<ControlTemplate.Triggers> <trigge          R property= "Button.ismouseover" value= "True" > <setter property= "button.foreground" value= "Red"/> </Trigger> </ControlTemplate.Triggers> <!--defining triggers _end--> </controltemplate&gt  ; </Window.Resources> Call Template: <button content= "Test btn" template= "{StaticResource buttontemplate}"/>3). Data templates "The same template just binds the data datatype" "Use data List" First step: Define data Namespace Datanamespace{public class dataclass{//Data list, Array, Model ...} Step two: XAML reference space xmlns:data= "Clr-namespace:datanamespace" Step Three: Call category <datatemplate x:key= "Datat" datatype= "{x:type demo :D Ataclass} "> Fourth step: Call Data source=" {Binding number"Fifth step: Call template itemtemplate=" {StaticResource Datat} "sixth step: Specify data Source public MainWindow () {InitializeComponent (); List<people> cars = new list<people> () {New people () {name= "Yang Jia", age= "10"} , New people () {name= "Li Jia", Age= "}, New People () {name=" Wu Jia "," age= "}, New P            Eople () {name= "Guo Jia", age= "40"}};        This.lbxControl.ItemsSource = cars;        Example: Setting personal information First step: Define the Data object namespace datanamespace{public class DataClass {public string name {set; get;}    public string Age {set; get;} }} Second step: XAML reference Space xmlns:data= "Clr-namespace:datanamespace" Step Three: Data template call category <Window.Resources> <datatemplate x:                    key= "Datat" datatype= "{x:type Demo:dataclass}" > <Grid> <Grid.ColumnDefinitions>                <columndefinition width= "Auto"/> <columndefinition width= "Auto"/> </Grid.columndefinitions> <textblock text= "{Binding name}" grid.column= "0"/> <tex Tblock text= "{Binding age}" grid.column= "1"/> </Grid> </DataTemplate> </window.res  Ources> Fourth Step: Call Data source= "{Binding data}" fifth step: Control call Template <listbox x:name= "Lbxcontrol" itemtemplate= "{StaticResource            Datat} "/> Sixth step: Bind data Source public MainWindow () {InitializeComponent ();                List<people> cars = new list<people> () {New people () {name= "Yang Jia", age= "10"}, New people () {name= "Li Jia", Age= "}, New People () {name=" Wu Jia ", age=" {"}, new People () {n            Ame= "Guo Jia", age= "40"};        This.lbxControl.ItemsSource = cars; }WPF Converter IValueConverter Interface//Convert to Picture public class Imagespathconverter:ivalueconverter {public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo cultuRE) {string uristr = string. Format (@ "Images/{0}.jpg", value.            ToString ());        return new BitmapImage (New Uri (Uristr, urikind.relative)); public object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo cultu        RE) {throw new NotImplementedException (); }}hierarchicaldatatemplate hierarchical data template 

WPF Quick Mastery Edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.