VS编程,WPF中,后台C#代码设置Grid控件某一行、某一列高度或者宽度的一种方法

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/82911798

有时为了显示的需要,想在某个操作后将Grid中的一列的宽度变成0或者根据内容自动调整。

这里提供了一个方法,可在后台代码中对Grid的行或者列进行调节。

 1、XAML前台中,给Grid的列起一个名字

       <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"  Name="GridTest"/>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

2、在相应的事件中,加入代码

  • 列宽度根据内容自动调整
GridTest.Width = System.Windows.GridLength.Auto;
  • 列宽度为 0
GridTest.Width = new System.Windows.GridLength(0);

可以将其中的  0  更改为其它任意数值,以定义长度。

      private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
       
         {
             //GridTest.Width = new System.Windows.GridLength(0);

               GridTest.Width = System.Windows.GridLength.Auto;
         }

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/82911798
今日推荐