WPF(一)举例讲解XML语法和格式

来源:互联网 发布:.top新网域名有哪些 编辑:程序博客网 时间:2024/05/19 00:14

WPF(一)举例讲解XML语法和格式

 

XAML是一种陈述性语言类似XML

 

XAML文件通常是以.xaml为后缀。

 

我们主要讲一下具体用法,拿个例子来讲,一下就懂了。

VS2008为新建WPF应用程序为例子.

新建项目后,自动为第一个窗体生成XAML文件。

具体生成内容入下:

<Window x:Class="Language.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">

    <Grid>

       

    </Grid>

</Window>  

基本格式就这样,内容一目了然,前面定义个  <Grid>,后面就必须已</Grid>结尾,也可以写成<Grid />

1:<Window></Window>

    表示一个WINDOW窗体

2:Xmlns相当与using,可以拿来添加命名空间

比如:xmlns:mynamespace="clr-namespace:DataTemplate" 这里就调用了DataTemplate命名空间,就可以用它了,用法: DataType="{x:Type mynamespace:Country},这里mynamespace就是我们上面给DataTemplate重新定个名字,可以随便取,我们最好是定义统一的标记,以后才认得倒,比如前面加_sp, Country就是DataTemplate命名空间下的一个类

3:<Grid></Grid>

这里是在WINDOW窗体里的一个Grid因为他是写在WINDOW里面的,Grid是布局用的,反正是个控件,可以添加其他的.比如:

我们在Grid里面再添加一个BUTTON

可以这样写

<Grid>

<Button></Button>

</Grid>

也可以这样写,都一个意思

<Grid>

<Button />

</Grid>

不想添加到Grid里面,想新添加Grid里面和Button在同一位置

    <Grid/>

        <Grid/>

        <Button/>

    </Grid>

4: <Window Title="Window1" Height="300" Width="300">

    这里的Title Height Width 就是定义的属性了,我们再用上面GridButton举例

<Grid Height="300" Width="300">

<Button Height="200" Width="100"></Button>

</Grid>

最后,我们再看下XAML的格式和语法

<Window x:Class="Language.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">

    <StackPanel Height="50" VerticalAlignment="Top">

        <TextBox Text="HELLO WORD!" ></TextBox>

        <Button Height=" 30" Name="bt"/>

    </StackPanel>

</Window>  

原创粉丝点击