WPF 入门笔记之控件内容控件

一、控件类

  在WPF中和用户交互的元素,或者说。能够接受焦点,并且接收键盘鼠标输入的元素所有的控件都继承于Control类。

1. 常用属性:

1.1 Foreground:前景画刷/前景色(文本颜色)

1.2 Background:背景画刷/背景色

//使用RBG设置颜色
Btn_1.Foreground = new SolidColorBrush(Color.FromRgb(120, 157, 200));
//使用颜色枚举设置颜色
Btn_1.Foreground = new SolidColorBrush(Colors.DarkGoldenrod);
//使用系统颜色枚举
Btn_1.Background = SystemColors.ActiveCaptionBrush;

1.3 FontFamily:设置字体

当FontFamily设置多个值表示,如果第一个字体不存在则使用第二个字体以此类推

<Button Name="Btn_1" FontFamily="Arial,Calibri,Cambria" >
     <Button.Content>
       this is button
     </Button.Content>
</Button>

系统上安装的字体:Fonts.SystemFontFamilies

foreach (var item in Fonts.SystemFontFamilies)
{
    lstBox_Message.Items.Add(item.Source);
}    

设置任何元素的字体属性,属性的值会向下继承。如果给Window顶层元素设置字体,那么这个window下的所有元素都会继承这个字体

1.4 FontSize:字体大小

1.5 FontStyle:字体样式(斜体)

1.6 FontWeight:字体粗细

1.7 FontStretch:字体可以拉伸的角度

<StackPanel Orientation="Horizontal">
      <Button Name="Btn_1" BorderBrush="Black" BorderThickness="2" Content="Border"></Button>
      <Button Name="Btn_2" Content="FontStyle" FontStyle="Oblique" FontWeight="Black"></Button>
      <Button Name="Btn_3" Content="FontWeight" FontStyle="Oblique"></Button>
      <Button Name="Btn_4" Content="FontSize" FontSize="15"></Button>
      <Button Name="Btn_5" Content="Fimaly" FontFamily="Arial"></Button>
</StackPanel>

1.8 BorderThickness:边框的大小

1.9 BorderBrush:边框画刷(边框颜色)

<Button Name="Btn_1" BorderBrush="Black" BorderThickness="2">
       this is button
</Button>

1.10 Padding:内边距

1.11 HorizontalContentAlignment:内容水平方向的位置

1.12 VerticalContentAlignment:内容垂直方向的位置

1.13 Template:模板

1.14 IsTabStop:否将某个控件包含在 Tab 导航中

1.15 TabIndex:决定在用户使用 Tab 键在控件中导航时元素接收焦点的顺序

二、内容控件

  内容控件,它可以包含显示一块内容。只能包含当个(只能包含的一个子元素)。和布局容器不同,布局容器可以不限制的包含子元素。内容控件都继承于ContentControl类

1. Content属性

Conent 只接收单一的对象(任何类型)

         <StackPanel>
            <!--直接设置Content属性-->
            <Button Content="button" Margin="5"></Button>
            <!--嵌套一个子元素-->
            <Button Margin="5">
                <TextBlock>this is TextBlock</TextBlock>
            </Button>
            <!--包含一个布局元素来实现嵌套多个子元素-->
            <Button Margin="5">
                <StackPanel>
                    <Label BorderBrush="Black" BorderThickness="1">this is Label</Label>
                    <Label BorderBrush="Aquamarine" BorderThickness="1" >this is Label</Label>
                    <Label BorderBrush="Beige" BorderThickness="1">this is Label</Label>
                </StackPanel>
            </Button>
        </StackPanel>

2. 设置对齐方式和ToolTip

HorizontalContentAlignment:水平对齐

VerticalContentAlignment:垂直对齐

ToolTip:鼠标移动到该元素时,显示提示内容

<Button Content="button" ToolTip="ToolTip case" Margin="5" HorizontalContentAlignment="Left" ></Button>
<Button Content="button" Margin="5" HorizontalContentAlignment="Right" ></Button>

3. 带标题的内容控件

  3.1 GroupBox 

          <GroupBox Header="GroupBox">
                    <StackPanel>
                        <RadioButton Margin="2">RadioButton1</RadioButton>
                        <RadioButton Margin="2">RadioButton2</RadioButton>
                        <RadioButton Margin="2">RadioButton3</RadioButton>
                    </StackPanel>
                </GroupBox>

  3.2 TabItem 

     <TabControl>
            <TabItem Header="TabItem"></TabItem>
            <TabItem Header="GroupBox"></TabItem>
            <TabItem Header="Expander"></TabItem>
    </TabControl>

  3.3 Expander

          <StackPanel>
                    <Expander Header="Expander">
                        <StackPanel>
                            <RadioButton Margin="2">RadioButton1</RadioButton>
                            <RadioButton Margin="2">RadioButton2</RadioButton>
                            <RadioButton Margin="2">RadioButton3</RadioButton>
                        </StackPanel>
                    </Expander>
                </StackPanel>
原文地址:https://www.cnblogs.com/haosit/p/8972164.html