首页 > 代码库 > WPF Demo4

WPF Demo4

<Window x:Class="Demo4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Demo4"
        Title="{x:Static local:MainWindow.TitleTest}" Height="500" Width="525">
    <!--窗体级别资源-->
    <Window.Resources>
        <Style x:Key="{x:Type Label}" TargetType="{x:Type Label}">
            <Setter Property="FontStyle" Value="http://www.mamicode.com/Italic"/>
            <Setter Property="Foreground" Value="http://www.mamicode.com/Red"/>
            <Setter Property="FontSize" Value="http://www.mamicode.com/36"/>
        </Style>
    </Window.Resources>
    <Grid ShowGridLines="True" UseLayoutRounding="True">
        <!--定义行数-->
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <!--定义列数-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <Label Content="{x:Static local:MainWindow.ageTest}" />
            <!--样式取值原则-就近原则-->
            <Label  Content="{x:Static local:MainWindow.nameTest}" FontSize="16"/>
            <Border BorderBrush="#FFE35A76" BorderThickness="10" Height="100" Name="border1" Width="200" Background="#FFEFE5E5" CornerRadius="10" DataContext="{Binding}" >
                <Label  Content="{x:Static local:MainWindow.ageTest}" />
            </Border>
           
            <UniformGrid >
                <Button Content="Window1" Click="Button_Click"/>
                <Button Content="b"/>
                <Button Content="c"/>
                <Button Content="d"/>
            </UniformGrid>
        </StackPanel>

        <StatusBar  Name="statusBar1" Grid.Row="2" Grid.ColumnSpan="3">
            <StatusBarItem Content="Status Bar"/>
        </StatusBar>
    </Grid>

</Window>

  

using System.Windows;

namespace Demo4
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string nameTest = "name:kunkun";
        public static string ageTest = "age:27";
        public static string TitleTest = "Static用法学习";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window1 w = new Window1();
            w.Topmost = true;
            w.ShowDialog();
        }
    }
}

  

<Window x:Class="Demo4.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>
        <DockPanel LastChildFill="True">
            <Button DockPanel.Dock="Top" Content="Top"/>
            <Button DockPanel.Dock="Bottom" Content="Bottom"/>
            <Button DockPanel.Dock="Left" Content="Left"/>
            <Button DockPanel.Dock="Right" Content="Right"/>
            <Button  Content="Middle"/>
        </DockPanel>
    </Grid>
</Window>

运行效果如下:

技术分享

注意:Grid的列宽与行高可采用固定、自动、按比列三种方式定义 。

UniformGrid 

UniformGrid 就是Grid的简化版,每个单元格的大小相同,不需要定义行列集合。

每个单元格始终具有相同的大小,每个单元格只能容纳一个控件,将自动按照定义在其内部的元素个数,自动创建行列,并通常保持相同的行列数。

UniformGrid 中没有Row 和Column 附加属性,也没有空白单元格。

WPF Demo4