第一章 第一课 Using WPF Control

■ 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
一、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
ContractedBlock.gif ExpandedBlockStart.gif 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 across 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; }
  • 二、Label Control and Mnemonic Keys
  • Labels contain built-in support for
mnemonic keys, which move the focus to a designated
control when 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 setting the Target property 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.

三、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" VerticalAlignment="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" VerticalAlignment="Top" Width="76">Button 3</RadioButton>
<RadioButton GroupName="Group2" Name="RadioButton4" Margin="15,85,0,0" Height="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="76">Button 4</RadioButton>

==

<StackPanel Height="29" VerticalAlignment="Top">
<RadioButton Name="RadioButton1">Button 1</RadioButton>
<RadioButton Name="RadioButton2">Button 2</RadioButton>
</StackPanel>
<StackPanel Height="34" Margin="0,34,0,0" VerticalAlignment="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: 图片原始尺寸

Fill:适应ImageControl大小

Uniform:等比缩放适应ImageControl,ImageControl不一定被填满,但图片不会被剪切

UniformFill:等比缩放,并填充ImageControl。图片可能会被剪切

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 receiving focus when the user presses the Tab key

Item Controls

ListBox

<ListBox Name="listbox1" VerticalAlignment="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" VerticalAlignment="Top">
<ToolBar Name="toolBar1" Height="26" VerticalAlignment="Top">
<Button>Back</Button>
<Button>Forward</Button>
<Button>Stop</Button>
</ToolBar>
<ToolBar>
<TextBox Width="100"/>
<Button>Go</Button>
</ToolBar>
</ToolBarTray>
 
Layout Controls

转载于:https://www.cnblogs.com/NewLand/archive/2011/05/31/2065203.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值