zoukankan      html  css  js  c++  java
  • 实现控件WPF(4)----Grid控件实现六方格

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!

         利用Grid控件能很轻松帮助我们实现各种布局。上面就是一个通过Grid单元格的分别实现的一个六方格的例子。

        xaml代码如下:

    <Window x:Class="WpfColorGrid.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="270" Width="464" Loaded="Window_Loaded">
      
        <Grid Name="grid1" Width="auto" Height="auto" Background="#FF06092B">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
            <Label Background="Red" Margin="0,0,0,0" Grid.Column="0" Grid.Row="0">Block 1</Label>
            <Label Background="SkyBlue" Margin="0,0,0,0" Grid.Column="1" Grid.Row="0">Block 2</Label>
            <Label Background="RosyBrown" Margin="0,0,0,0" Grid.Column="2" Grid.Row="0">Block 3</Label>
            <Label Background="SpringGreen" Margin="0,0,0,0" Grid.Column="0" Grid.Row="1">Block 4</Label>
            <Label Background="RoyalBlue" Margin="0,0,0,0" Grid.Column="1" Grid.Row="1">Block 5</Label>
            <Label Background="Violet" Margin="0,0,0,0" Grid.Column="2" Grid.Row="1">Block 6</Label>
            </Grid>
    </Window>

        其中,以下代码将一个Grid单元格分别为2行3列的六方格,本例中已将前几个单元格的Width 和Height设成固定值了;当Column的宽度或者Row的高度想通过内部元夙来决定的时,将其设置为 Auto; 当某Column的宽度或者某Row的高度想根据窗口的残余大小来自动调整时,将其设置成 *;当希望其中一项的长度是另外一项的2倍,将其设成2* 。

        每日一道理
    生活中受伤难免,失败跌倒并不可怕,可怕的是因此而一蹶不振,失去了对人生的追求与远大的理想。没有一个人的前进道路是平平稳稳的,就算是河中穿梭航行的船只也难免颠簸,生活中所遇上的坎坷磨难不是偶尔给予的为难,而是必然所经受的磨练。
    <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

        

        xaml.cs代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfColorGrid
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
    
            }
        }
    }

        

        

        运行程序,效果如下:

        实现和控件

        

        ShowGridLines="True"添加以后:

    <Grid Name="grid1" Width="auto" Height="auto" ShowGridLines="True" Background="#FF06092B">

        将表现边框线,效果如下:

        实现和控件

    文章结束给大家分享下程序员的一些笑话语录: 3G普不普及现在已经不是看终端了,而是看应用,有好的,便宜实用的应用,花1000多买个能用的智能手机应该不是什么难事。反过来说,你200元拿一个智能手机,没有好的应用,看个电影要几十元,也是没人用3G。

    --------------------------------- 原创文章 By
    实现和控件
    ---------------------------------

  • 相关阅读:
    bzoj2959
    学习笔记::lct
    bzoj3203
    bzoj1319
    bzoj3625
    bzoj3992
    bzoj1565
    bzoj3513
    平常练习动归(1.胖男孩)———最长公共子序列
    2016 noip 复赛 day2
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3150342.html
Copyright © 2011-2022 走看看