zoukankan      html  css  js  c++  java
  • C#编程之WPF控件开发(八)

    WPF控件指任何代表应用程序中可见对象的类(类不必从control类继承,即可具有可见外观),e.g Button, TextBox etc.。从Control类继承的类包含一个ControlTemplate,允许控件的使用方在无需创建新子类的情况下根本改变控件的外观。

    创建控件:可以通过使用AXML或以代码形式向应用程序添加控件。

    下面我仅对使用AXML添加控件进行讲解,对于代码形式,相对来说复杂了一点。这里我并不说,代码形式不好,而是说,当我们开发项目的时候,工作效率是大多数人优先考虑的。这里也是一样。同样,有兴趣的也可以自己尝试一下。

    首先我们将Grid布局为3行(高度30)2列(宽度默认与对话框一样宽)

     1 <Grid>
     2         <Grid.RowDefinitions>
     3             <RowDefinition Height="30"/>
     4             <RowDefinition Height="30"/>
     5             <RowDefinition Height="30"/>
     6         </Grid.RowDefinitions>
     7         <Grid.ColumnDefinitions>
     8             <ColumnDefinition/>
     9             <ColumnDefinition/>
    10         </Grid.ColumnDefinitions>
    11     </Grid>
    View Code

    这里直接添加label,其位置默认为 Grid.Row="0" Grid.Column="0" 所以其可以写为: <Label Grid.Row="0" Grid.Column="0" Content="Entry you first name:" /> 或者 <Label Content="Entry you first name:" /> 或者 <Label Grid.Column="0"> Entry your First Name: </Label> 或者 <Label Grid.Column="0" Grid.Row="0"> Entry your First Name: </Label> 其结果是一样的。

    接着在第0行第1列添加TextBox控件,并设置其参数:name=firstName。

    <TextBox Grid.Row="0" Grid.Column="1" Name="firstName" Margin="0,5,10,5"/>

    Margin布局属性可以设置到边框距离,表示到边框距左上右下分别为0,5,10,5.

    另外如果参数只有一个,表示到各个边距离都为指定的参数,如果设置参数为10,代表到各个边到边框距离为10:

    <Button Grid.Row="2" Grid.Colum="1" Name="Submit" Margin="10"/>

    同样的方法添加其他的控件:

    <Window x:Class="WpfControls.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0">Entry your First Name:</Label>
            <TextBox Grid.Row="0" Grid.Column="1" Name="firstName" Margin="0,5,10,5"/>
            <Label Grid.Row="1">Entry your Last Name:</Label>
            <TextBox Grid.Column="1" Grid.Row="1" Name="lastName" Margin="0,5,10,5"/>
            <Button Grid.Row="2" Grid.Column="0" Name="submit" Margin="2">View message</Button>
            <Button Grid.Row="2" Grid.Column="2" Name="Clear" Margin="2">Clear Name</Button>
        </Grid>
    </Window>
    View Code

    到目前为止,我们所有的代码都没添加注释,AXML注释的添加是通过尖括号来添加的格式: <!-- Add your Noted here: -->  

    接下来我们说一下如何改变控件的外观。

    要实现自己的想要的效果,可以通过执行以下操作之一来更改控件外观:

    • 更改控件的属性值。
    • 为控件创建Style。
    • 为控件创建ControlTemplate。
          <Button Grid.Row="2" Grid.Column="0" Name="submit" Margin="2">
                <Button.Background>
                    <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                        <GradientStop Color="Green" Offset="0.0"/>
                        <GradientStop Color="White" Offset="0.9"/>
                    </LinearGradientBrush>
                </Button.Background>
                View message</Button>

    其中 <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> 线性渐变色笔刷二维起始坐标与二维终止坐标

     <GradientStop Color="Green" Offset="0.0"/> 渐变过度点的为止与颜色...

    同样其他的控件也可以做出自己想要的效果来。

    End

     谢谢.

  • 相关阅读:
    BLAST
    用 python 实现各种排序算法(转)
    纠错工具之
    《生物序列分析》
    比对软件
    MySQL版本升级参考资料【转】
    解决mysql开启GTID主从同步出现1236错误问题【转】
    Linux系统打开core dump的配置【转】
    MySQL在线更改binlog格式
    关于MySQL 8.0的几个重点【转】
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/11897914.html
Copyright © 2011-2022 走看看