■ Explain what a content control is.
■ Describe and use several common WPF controls.
■ Use a Dependency Property.
■ Create and use an item control in your user interface.
■ Create a menu.
■ Create a toolbar.
■ Create a status bar.
■ Explain the properties of a control that manage layout.
■ Explain how to use the grid control.
■ Explain how to use the uniformgrid control.
■ Explain how to use the stackpanel control.
■ Explain how to use the wrappanel control.
■ Explain how to use the dockpanel control.
■ Explain how to use the canvas control.
■ Configure control sizing.
■ Align content at design time.
■ Use the gridsplitter control. WPF control Individual controls Item Control Layout controls 1. content controls: lable, button, checkbox, radiobutton
- 1) derives from the contentcontrol and has content Property
- 2) The type of the content property is object, so it can accept any object as content. How
Content is rendered, however, depends on the type of the object in the content Property
- For items that do not derive from uielement, The tostring method is called, and the resulting
String is rendered as the control content. Items that derive from uielement, however, are
Displayed as contained within the content control
- 3) content controls can contain only a single nested element there is no inherent limit on the number of nested elements that the content can contain in
View Co
// Summary:
// Represents the base class for all system. Windows. Controls. button controls.
[Defaultevent ( " Click " )]
[Localizability (localizationcategory. Button)]
Public Abstract Class Buttonbase: contentcontrol, icommandsource // Summary: // Represents a control with a single piece of content of any type. [contentproperty ("content")] [Localizability (localizationcategory. none, readability = readability. unreadable)] [defaultproperty ("content")] public class contentcontrol: control, iaddchild { // Summary: // Identifies the system. Windows. Controls. contentcontrol. Content dependency // Property. // // Returns: // The identifier for the system. Windows. Controls. contentcontrol. Content dependency // Property. [commondependencyproperty] public static readonly dependencyproperty contentproperty; // // Summary: // Identifies the system. Windows. Controls. contentcontrol. contentstringformat // Dependency Property. // // Returns: // The identifier for the system. Windows. Controls. contentcontrol. contentstringformat // Dependency Property. [commondependencyproperty] public static readonly dependencyproperty contentstringformatproperty; // // Summary: // Identifies the system. Windows. Controls. contentcontrol. contenttemplate dependency // Property. // // Returns: // The identifier for the system. Windows. Controls. contentcontrol. contenttemplate dependency // Property. [commondependencyproperty] public static readonly dependencyproperty contenttemplateproperty; // // Summary: // Identifies the system. Windows. Controls. contentcontrol. contenttemplateselector dependency // Property. // // Returns: // The identifier for the system. Windows. Controls. contentcontrol. contenttemplateselector dependency // Property. [commondependencyproperty] public static readonly dependencyproperty contenttemplateselectorproperty; // // Summary: // Identifies the system. Windows. Controls. contentcontrol. hascontent dependency // Property. // // Returns: // The identifier for the system. Windows. Controls. contentcontrol. hascontent dependency // Property. [commondependencyproperty] public static readonly dependencyproperty hascontentproperty; // Summary: // Initializes a new instance of the system. Windows. Controls. contentcontrol // Class. [targetedpatchingoptout ("performance critical to inline this type of method defined SS ngen image boundaries")] public contentcontrol (); // Summary: // Gets or sets the content of a system. Windows. Controls. contentcontrol. //// Returns: // An object that contains the control's content. The default value is null. [Bindable (true)] [customcategory ("content")] public object content {Get; set ;} //// Summary: // Gets or sets a composite string that specifies how to format the system. Windows. Controls. contentcontrol. Content // Property if it is displayed as a string. //// Returns: // A composite string that specifies how to format the system. Windows. Controls. contentcontrol. Content // Property if it is displayed as a string. [Bindable (true)] [customcategory ("content")] Public String contentstringformat {Get; set ;} //// Summary: // Gets or sets the data template used to display the content of the system. Windows. Controls. contentcontrol. //// Returns: // A data template. The default value is null. [Bindable (true)] [customcategory ("content")] public datatemplate contenttemplate {Get; set ;} //// Summary: // Gets or sets a template selector that enables an application writer to provide // Custom template-selection logic. //// Returns: // A Data Template selector. the default value is null. [customcategory ("content")] [Bindable (true)] [designerserializationvisibility (designerserializationvisibility. hidden)] public datatemplateselector contenttemplateselector {Get; set ;} //// Summary: // Gets a value that indicates whether the system. Windows. Controls. contentcontrol // Contains content. //// Returns: // True if the system. Windows. Controls. contentcontrol has content; otherwise // False. The default value is false. [readonly (true)] [browsable (false)] public bool hascontent {Get ;} //// Summary: // Gets an enumerator to the content control's logical child elements. //// Returns: // An enumerator. The default value is null. Protected internal override ienumerator logicalchildren {Get ;}
2. Label control and mnemonic keys
Labels contain built-in support Mnemonic keys, Which Move the focus to a designated
ControlWhen the Alt key is pressed with the mnemonic key.
The mnemonic key is specified by preceding the ddesired key with the underscore (_) symbol and appears underlined at run time when the alt
Key is pressed. We can designate a target control by settingTargetProperty of the label Control
Sample Code:
<Label name = "label1" width = "100" target = "{binding elementname =Testbox1} "> Press Alt + _ A </label>
<Textbox name ="Testbox1"Height =" 26 "width =" 100 "> </textbox>
When user press Alt + A, the input focus will move to textbox.
3. Button Control
Isdefault property and the iscancel Property
1) default Property
When isdefault is set to true, the button's click event is raised
Ou press ENTER
2) iscancel Property
When iscancel is set to true, the button's click event is raised
When ESC is pressed.
3) access keys:
The button will be clicked when the user presses ALT and that key together
<Button> _ click me! </Button>
Radiobutton Control
You can have multiple groups in a single container by setting the groupname Property
<Radiobutton groupname = "group1" name = "radiobutton1" Height = "22" verticalignment = "TOP" margin = "15,10, 0, 0 "horizontalalignment =" Left "width =" 76 "> button 1 </radiobutton>
<Radiobutton groupname = "group1" name = "radiobutton2" margin = "15,34, 0, 0 "Height =" 22 "verticalalignment =" TOP "horizontalalignment =" Left "width =" 76 "> button 2 </radiobutton>
<Radiobutton groupname = "Group2" name = "radiobutton3" margin = "15,58, 0, 0 "Height =" 21 "horizontalalignment =" Left "verticalignment =" TOP "width =" 76 "> button 3 </radiobutton>
<Radiobutton groupname = "Group2" name = "radiobutton4" margin = "15,85, 0, 0 "Height =" 22 "horizontalalignment =" Left "verticalignment =" TOP "width =" 76 "> button 4 </radiobutton>
=
<Stackpanel Height = "29" verticalignment = "TOP">
<Radiobutton name = "radiobutton1"> button 1 </radiobutton>
<Radiobutton name = "radiobutton2"> button 2 </radiobutton>
</Stackpanel>
<Stackpanel Height = "34" margin = "," verticalignment = "TOP">
<Radiobutton name = "radiobutton3"> button 3 </radiobutton>
<Radiobutton name = "radiobutton4"> button 4 </radiobutton>
</Stackpanel>
Checkbox control
The ischecked property is a Boolean? (Bool? In C #) Data Type similar to the boolean type but allows an indeterminate state as well. A check
Box will be in the indeterminate state when a window first opens.
Other control:
Textblock
Image:
Scretch: None, fill, uniform, uniformfill
None: original image size
Fill: Suitable for imagecontrol
Uniform: proportional scaling adapts to imagecontrol. imagecontrol is not necessarily filled, but the image is not cut.
Uniformfill: proportional scaling, and filling in imagecontrol. The image may be cut.
Progressbar
Textbox
Slider bar
Setting the tab order for controls
By default, controls in the user interface will receive the focus
From Tab key presses in the order in which they are defined in the XAML
Tabindex
Keyboardnavigation. istabstop = "false" to keep a control from grouping ing focus when the user presses the tab key
Item controls
ListBox
<ListBox name = "listbox1" verticalignment = "TOP">
<Checkbox name = "CHK1"> Option 1 </checkbox>
<Checkbox name = "CHK2"> Option 2 </checkbox>
<Checkbox name = "chk3"> Option 3 </checkbox>
<Checkbox name = "chk4"> Option 4 </checkbox>
</ListBox>
<Treeview>
<Treeviewitem header = "pizza toppings">
<Checkbox content = "Pepperoni"/>
<Checkbox content = "sausage"/>
<Checkbox content = "mushroom"/>
<Checkbox content = "tomato"/>
Lesson 1: Using WPF controls Chapter 1 15
</Treeviewitem>
<Treeviewitem header = "sandwich items">
<Checkbox content = "lettuce"/>
<Checkbox content = "tomato"/>
<Checkbox content = "mustard"/>
<Checkbox content = "hot peppers"/>
</Treeviewitem>
</Treeview>
<Menu Height = "22" name = "menu1" verticalalignment = "TOP"
Horizontalalignment = "Left" width = "278">
<Menuitem header = "_ file">
<Menuitem header = "open"/>
<Menuitem header = "close"/>
<Menuitem header = "save" command = "applicationcommands. Save"/>
</Menuitem>
</Menu>
<Textbox>
<Textbox. contextmenu>
<Contextmenu>
<Menuitem header = "cut" command = "applicationcommands. Cut"/>
<Menuitem header = "copy" command = "applicationcommands. Copy"/>
<Menuitem header = "Paste" command = "applicationcommands. Paste"/>
</Contextmenu>
</Textbox. contextmenu>
</Textbox>
<Toolbartray name = "toolbartray1" Height = "65" verticalignment = "TOP"> <toolbar name = "toolbar1" Height = "26" verticalignment = "TOP"> <button> back </button> <button> forward </button> <button> stop </button> </toolbar> <textbox width = "100"/> <button> go </button> </toolbar> </toolbartray>
Layout controls