日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF Grid动态显示或隐藏一列的一种方法

發布時間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF Grid动态显示或隐藏一列的一种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目中有一個需求,需要根據用戶的設置動態顯示一列,研究了一波后,發現,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动态显示或隐藏一列的一种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。