RowDefinition的Height="*"和 ColumnDefinition的Width="*" 在C#中的写法

简介: ColumnDefinition.Width的数据类型是:GridLength,所以就可以写为: column1.Width = new GridLength(1, GridUnitType.Star); //column1为一个实例 如果ColumnDefinition的Width="Auto"   则在C#代码里就可以写为 column1.

 ColumnDefinition.Width的数据类型是:GridLength,所以就可以写为:

 column1.Width = new GridLength(1, GridUnitType.Star); //column1为一个实例

 如果ColumnDefinition的Width="Auto"   则在C#代码里就可以写为

 column1.Width =GridLength.Auto;

 这里不要搞混了, GridLength.Auto是一个结构,而 new GridLength(1, GridUnitType.Star);是一个类

 

下面来一个完整的布局代码,全部使用C#代码完成(练习的) 

  public  partial  class MainWindow : Window
    {
         public MainWindow()
        {
            InitializeComponent();

             // 显示网格
            grid1.ShowGridLines =  true;

             //
            RowDefinition row1 =  new RowDefinition();
            RowDefinition row2 =  new RowDefinition();

             //
            ColumnDefinition column1 =  new ColumnDefinition();
            ColumnDefinition column2 =  new ColumnDefinition();

             // 使用*号布局,当值为1的时候,表示*  值为2的时候,表示2*  就是说,第二列的宽度是第一列宽度的2倍
            column1.Width =  new  GridLength( 1, GridUnitType.Star); 
            column2.Width =  new  GridLength( 2, GridUnitType.Star);


            Button button1 =  new Button();
            Button button2 =  new Button();
            Button button3 =  new Button();
            Button button4 =  new Button();

             // 将行和列添加到Grid面板里
            grid1.RowDefinitions.Add(row1);
            grid1.RowDefinitions.Add(row2);

            grid1.ColumnDefinitions.Add(column1);
            grid1.ColumnDefinitions.Add(column2);

             // 将按钮添加到Grid面板里
            grid1.Children.Add(button1);
            grid1.Children.Add(button2);
            grid1.Children.Add(button3);
            grid1.Children.Add(button4);

            button1.Content =  " 1 ";
            button2.Content =  " 2 ";
            button3.Content =  " 3 ";
            button4.Content =  " 4 ";

             // 设置每个按钮所在的单元格中
            Grid.SetRow(button1,  0);
            Grid.SetColumn(button1,  0);

            Grid.SetRow(button2,  0);
            Grid.SetColumn(button2,  1);

            Grid.SetRow(button3,  1);
            Grid.SetColumn(button3,  0);

            Grid.SetRow(button4,  1);
            Grid.SetColumn(button4,  1);


        }
    }


 

目录
相关文章
|
弹性计算 网络协议 网络安全
在Windows Server系统上配置静态IP
在Windows Server系统上配置静态IP的方法
在Windows Server系统上配置静态IP
|
C#
WPF中实现多选ComboBox控件
原文:WPF中实现多选ComboBox控件 在WPF中实现带CheckBox的ComboBox控件,让ComboBox控件可以支持多选。 将ComboBox的ItemsSource属性Binding到一个Book的集合, public class Book { ...
3779 0
|
C# Windows
wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法:   1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.
5878 0
|
前端开发 C# Windows
WPF基础:在Canvas上绘制图形
WPF基础:在Canvas上绘制图形
339 0
|
开发框架 .NET C#
C# 一分钟浅谈:第一个 C# 控制台应用程序
【9月更文挑战第1天】C# 是一种现代化的、面向对象的编程语言,广泛应用于桌面应用、Web 应用和游戏开发等领域。本文详细介绍如何创建第一个 C# 控制台应用程序,包括使用 Visual Studio 和 .NET SDK 的步骤,并解析常见问题及其解决方法,如控制台窗口立即关闭、编译错误和运行时错误等。通过实践,你将掌握 C# 控制台应用的基础知识,为进一步学习打下坚实基础。
520 49
|
10月前
|
数据处理 C# Windows
WPF中实现弹出进度条窗口
【11月更文挑战第14天】在WPF中实现弹出进度条窗口,需创建进度条窗口界面(XAML)和对应的代码-behind(C#)。通过定义`ProgressWindow`类,包含`ProgressBar`和`TextBlock`,并在主窗口或逻辑代码中调用,模拟长时间任务时更新进度条,确保UI流畅。
420 0
修改apt-get源为国内镜像源
修改apt-get源为国内镜像源
5434 0
|
C++ 计算机视觉
实用分享-Dependencies(DLL解析工具)
实用分享-Dependencies(DLL解析工具)
3714 0
|
人工智能 物联网 Linux
手把手之如何在嵌入式Linux上运行QT应用程序(以百问网imx6ull开发板为例)
手把手之如何在嵌入式Linux上运行QT应用程序(以百问网imx6ull开发板为例)
1608 0
|
传感器 编解码 算法
Halcon XLD: eXtended Line Descriptions 亚像素轮廓
Halcon XLD: eXtended Line Descriptions 亚像素轮廓
1083 0
Halcon XLD: eXtended Line Descriptions 亚像素轮廓

热门文章

最新文章