项目中有一个需求,需要根据用户的设置动态显示一列,研究了一波后,发现,Grid并没有这个功能,于是通过绑定宽度 的方法,实现的需求。。

思路:将需要隐藏的列宽度 绑定到一个属性上,隐藏时就设置宽度为0

写了个Demo,思路是一样的,看看示例效果吧:

下面就粘上xaml和代码喽:

MainWindow.xaml:

<Window x:Class="wpfcore.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:wpfcore" xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"mc:Ignorable="d"Background="LightBlue"UseLayoutRounding="True"FontSize="20"Title="MainWindow" Width="600" Height="400"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><StackPanel Orientation="Horizontal"><Button Content="显示Name列" Click="Button_Click"/><Button Content="显示Age列" Click="Button_Click_1"/><Border Width="20"/><Button Content="隐藏Name列" Click="Button_Click_2"/><Button Content="隐藏Age列" Click="Button_Click_3"/></StackPanel><Grid Grid.Row="1" Margin="5"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="{Binding NameWidth}"/><ColumnDefinition Width="{Binding AgeWidth}"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="auto"/></Grid.RowDefinitions><TextBlock Text="编号" Grid.Row="0" Grid.Column="0"/><TextBlock Text="姓名" Grid.Row="0" Grid.Column="1"/><TextBlock Text="年龄" Grid.Row="0" Grid.Column="2"/><TextBlock Text="爱好" Grid.Row="0" Grid.Column="3"/><TextBlock Text="1" Grid.Row="1" Grid.Column="0"/><TextBlock Text="张三" Grid.Row="1" Grid.Column="1"/><TextBlock Text="18" Grid.Row="1" Grid.Column="2"/><TextBlock Text="玩游戏" Grid.Row="1" Grid.Column="3"/><TextBlock Text="2" Grid.Row="2" Grid.Column="0"/><TextBlock Text="张三" Grid.Row="2" Grid.Column="1"/><TextBlock Text="18" Grid.Row="2" Grid.Column="2"/><TextBlock Text="玩游戏" Grid.Row="2" Grid.Column="3"/><TextBlock Text="3" Grid.Row="3" Grid.Column="0"/><TextBlock Text="张三" Grid.Row="3" Grid.Column="1"/><TextBlock Text="18" Grid.Row="3" Grid.Column="2"/><TextBlock Text="玩游戏" Grid.Row="3" Grid.Column="3"/><TextBlock Text="3" Grid.Row="4" Grid.Column="0"/><TextBlock Text="张三" Grid.Row="4" Grid.Column="1"/><TextBlock Text="18" Grid.Row="4" Grid.Column="2"/><TextBlock Text="玩游戏" Grid.Row="4" Grid.Column="3"/></Grid></Grid>
</Window>

MainWindow.cs:

using System.Windows;
using System.Windows.Media;namespace wpfcore
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext = this;}public GridLength NameWidth{get { return (GridLength)GetValue(NameWidthProperty); }set { SetValue(NameWidthProperty, value); }}public static readonly DependencyProperty NameWidthProperty =DependencyProperty.Register("NameWidth", typeof(GridLength), typeof(MainWindow), new PropertyMetadata(new GridLength(1,GridUnitType.Star)));public GridLength AgeWidth{get { return (GridLength)GetValue(AgeWidthProperty); }set { SetValue(AgeWidthProperty, value); }}public static readonly DependencyProperty AgeWidthProperty =DependencyProperty.Register("AgeWidth", typeof(GridLength), typeof(MainWindow), new PropertyMetadata(new GridLength(1, GridUnitType.Star)));private void Button_Click(object sender, RoutedEventArgs e){NameWidth = new GridLength(1, GridUnitType.Star);}private void Button_Click_1(object sender, RoutedEventArgs e){AgeWidth = new GridLength(1, GridUnitType.Star);}private void Button_Click_2(object sender, RoutedEventArgs e){NameWidth = new GridLength(0, GridUnitType.Star);}private void Button_Click_3(object sender, RoutedEventArgs e){AgeWidth = new GridLength(0, GridUnitType.Star);}}
}

ok.完成喽

如果喜欢,点个赞呗~

WPF Grid动态显示或隐藏一列的一种方法相关推荐

  1. jquery控制元素的隐藏和显示的几种方法。

    使用jquery控制div的显示与隐藏,一句话就能搞定,例如: 1.$("#id").show()表示为display:block, $("#id").hide ...

  2. excel 删除空白列的三种方法,以及如何批量删除空行空列

    详见:excel删除空白列的三种方法,以及如何批量删除空行空列 本人更倾向于第三种方法: excel删除空白列方法三: 按ALT+F11,打开VBE编辑器,插入--模块,复制下面代码,然后按F5键,运 ...

  3. mysql行转列sql函数_sql动态行转列的两种方法

    第一种方法: 代码如下: select *from ( select Url,case  when  Month=01 then  '1月' when  Month=02 then '2月' when ...

  4. WPF程序将DLL嵌入到EXE的两种方法

    2019独角兽企业重金招聘Python工程师标准>>> WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了> ...

  5. js设置控件的隐藏与显示的两种方法

    js设置控件的隐藏与显示的两种方法: js设置控件的隐藏与显示,设置控件style的display和visibility属性就可以了. 用JavaScript隐藏控件的方法有两种,分别是通过设置控件的 ...

  6. Spark SQL DataFrame新增一列的四种方法

    Spark SQL DataFrame新增一列的四种方法 方法一:利用createDataFrame方法,新增列的过程包含在构建rdd和schema中 方法二:利用withColumn方法,新增列的过 ...

  7. 【Axure交互教程】 隐藏页面滚动条的3种方法

    作品名称:隐藏页面滚动条的3种方法 作品编号:Case001 软件版本:Axure9 作品类型:交互案例 原型预览链接(附源文件下载链接):http://daisyaxure.com/demo/Cas ...

  8. Oracle 实现行转列的几种方法

    Oracle 实现行转列的几种方法 表数据 1 使用 decode 与聚合函数实现 2 使用 case when 与聚合函数实现 3 使用 pivot 函数 表数据 with students as( ...

  9. sql行转列的3种方法

    行转列的3种方法 max(case when) pivot() 用存储过程行转列 准备数据 --创建表 create table Table_A (商家 string,奶茶 string,价格 big ...

最新文章

  1. Hadoop Hive迁移至MaxCompute
  2. 【vue】webpack打包vue项目并且运行在Tomcat里面
  3. Albert: A lite bert for self-supervised learning of language representations (Albert)
  4. 1.16 字符串的加密解密
  5. boost使用split分割字符串
  6. python提示list index out of range_为什么python报错说“list index out of range”
  7. 洛阳计算机学校排名2015年,洛阳最好的中专学校有哪些 十大中专学校排名
  8. 【Cqoi2006】【BZOJ2222】猜数游戏(乱搞)
  9. SCUT - 290 - PARCO的因数游戏 - 博弈论
  10. 使用SQL2005 递归查询结合Row_Number()实现完全SQL端树排序
  11. python2升级_Python2 升级 Python3
  12. linux 蓝牙打印机驱动安装失败,Win7蓝牙驱动安装失败的原因分析与解决方法
  13. android 代码混淆 反编译,Android的反编译和代码混淆
  14. 用户自治的数字身份在我国的标准
  15. 计算机基础简明教案,计算机基础完整教案
  16. 个人博客或网站快速被搜索引擎收录
  17. Boost编译一步步来
  18. 长期宅在家的人都有什么比较好的室内锻炼的方法?
  19. 以太网通信连接不上自检步骤
  20. Socket在iOS客户端上的简单实现 - 利用GCAsyncSocket框架

热门文章

  1. [导入]数据库物理模型设计的其他模式之继承模式
  2. linux里查看最耗CPU的线程
  3. UI交互设计教程分享:提高界面交互体验的“葵花宝典”
  4. tomcat启动报错:Bean name 'XXX' is already used in this beans element
  5. animate 实现滑动切换效果
  6. Mac OSX使用VMware Fusion安装windows虚拟机教程
  7. 如何在PowerPoint中制作打字机或命令行动画
  8. 如何重新打开Windows防火墙提示?
  9. centos 7.x systemd service 配置方法整理
  10. 7z压缩文档的powershell示例