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

歡迎訪問 生活随笔!

生活随笔

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

asp.net

WPF Grid添加边框的两种方法

發布時間:2023/12/4 asp.net 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF Grid添加边框的两种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近項目中使用到了Grid表格,居然要加邊框,查了一下,grid原生居然是不支持實線邊框的。。

最終我還是實現了效果,

看看吧:

左邊是直接寫的每行一個border,每列寫一個border,這樣在行列比較少的時候還行,如果多了,那不慘了,項目中我就這樣寫的了,反正能實現效果了。。。

右邊是使用附加屬性,動態添加的border,還行吧,有不少缺點,比如不能處理單元格合并的問題。先不管了,反正我也用不到哈哈。

下面就看看代碼吧:

新建一個GridProperties類,用來 放附加屬性的定義:

using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace wpfcore {public class GridProperties{public static bool GetShowBorder(DependencyObject obj){return (bool)obj.GetValue(ShowBorderProperty);}public static void SetShowBorder(DependencyObject obj, bool value){obj.SetValue(ShowBorderProperty, value);}public static readonly DependencyProperty ShowBorderProperty =DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(Grid), new PropertyMetadata(false,OnShowBorderChanged));private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (!(d is Grid grid)) return;grid.Loaded += OnLoaded;}private static void OnLoaded(object sender, RoutedEventArgs e){if (!(sender is Grid grid)) return;var rowCount = grid.RowDefinitions.Count;var columnCount = grid.ColumnDefinitions.Count;var thickness = new Thickness(1);var bottomThickness = new Thickness(0,0,0,1);var rightThickness = new Thickness(0,0,1,0);var headerBack = new SolidColorBrush(Color.FromArgb(255, 129, 133, 145));for (int i = 0; i < rowCount; i++){Border border = new Border(){BorderBrush = Brushes.Black,BorderThickness = i == 0 ? thickness : bottomThickness,Background = i == 0 ? headerBack : Brushes.Transparent,};border.SetValue(Panel.ZIndexProperty, -1000);border.SetValue(Grid.RowProperty, i);border.SetValue(Grid.ColumnProperty, 0);border.SetValue(Grid.ColumnSpanProperty, columnCount);grid.Children.Add(border);}for (int i = 0; i < columnCount; i++){Border border = new Border(){BorderBrush = Brushes.Black,BorderThickness = i == 0 ? thickness : rightThickness,Background = Brushes.Transparent,};border.SetValue(Panel.ZIndexProperty, -1000);border.SetValue(Grid.RowProperty, 0);border.SetValue(Grid.ColumnProperty, i);border.SetValue(Grid.RowSpanProperty, rowCount);grid.Children.Add(border);}grid.Loaded -= OnLoaded;}} }

然后在MainWindow的測試代碼:

<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"mc:Ignorable="d"Background="LightBlue"UseLayoutRounding="True"Title="MainWindow" Width="820" Height="340"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><StackPanel Margin="30" VerticalAlignment="Center" HorizontalAlignment="Center"><TextBlock Text="直接寫 Border的方式添加" FontSize="18" Margin="10" Foreground="Green" HorizontalAlignment="Center"/><Grid Width="300" Height="150" ><Grid.Resources><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="VerticalAlignment" Value="Center"/><Setter Property="FontSize" Value="16"/><Setter Property="FontFamily" Value="微軟雅黑"/> </Style><Style TargetType="Border" ><Setter Property="BorderBrush" Value="Black"/><Setter Property="BorderThickness" Value="1"/> </Style></Grid.Resources><Grid.ColumnDefinitions><ColumnDefinition Width="1.5*"/><ColumnDefinition Width="1*"/><ColumnDefinition Width="2*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition /><RowDefinition /><RowDefinition /><RowDefinition /></Grid.RowDefinitions><Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Background="#818591"/><Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 0 0 1"/><Border Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 0 0 1"/><Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 0 0 1"/><Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" /><Border Grid.Row="0" Grid.Column="1" Grid.RowSpan="4" BorderThickness="0 0 1 0"/><Border Grid.Row="0" Grid.Column="2" Grid.RowSpan="4" BorderThickness="0 0 1 0"/><Border Grid.Row="0" Grid.Column="3" Grid.RowSpan="4" BorderThickness="0 0 1 0"/><TextBlock Grid.Row="0" Grid.Column="0" Text="姓名"/><TextBlock Grid.Row="0" Grid.Column="1" Text="年齡"/><TextBlock Grid.Row="0" Grid.Column="2" Text="興趣愛好"/><TextBlock Grid.Row="0" Grid.Column="3" Text="性別"/><TextBlock Grid.Row="1" Grid.Column="0" Text="WPF UI"/><TextBlock Grid.Row="1" Grid.Column="1" Text="3M"/><TextBlock Grid.Row="1" Grid.Column="2" Text="分享代碼"/><TextBlock Grid.Row="1" Grid.Column="3" Text="漢子"/></Grid></StackPanel><StackPanel Grid.Column="1" Margin="30" VerticalAlignment="Center" HorizontalAlignment="Center"><TextBlock Text="使用附加屬性添加" FontSize="18" Margin="10" Foreground="Green" HorizontalAlignment="Center"/><Grid Width="300" Height="150" local:GridProperties.ShowBorder="True"><Grid.Resources><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="VerticalAlignment" Value="Center"/><Setter Property="FontSize" Value="16"/><Setter Property="FontFamily" Value="微軟雅黑"/> </Style><Style TargetType="Border"><Setter Property="BorderBrush" Value="Black"/><Setter Property="BorderThickness" Value="1"/> </Style></Grid.Resources><Grid.ColumnDefinitions><ColumnDefinition Width="1.5*"/><ColumnDefinition Width="1*"/><ColumnDefinition Width="2*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition /><RowDefinition /><RowDefinition /><RowDefinition /><RowDefinition /></Grid.RowDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="姓名"/><TextBlock Grid.Row="0" Grid.Column="1" Text="年齡"/><TextBlock Grid.Row="0" Grid.Column="2" Text="興趣愛好"/><TextBlock Grid.Row="0" Grid.Column="3" Text="性別"/><TextBlock Grid.Row="1" Grid.Column="0" Text="WPF UI"/><TextBlock Grid.Row="1" Grid.Column="1" Text="3M"/><TextBlock Grid.Row="1" Grid.Column="2" Text="分享代碼"/><TextBlock Grid.Row="1" Grid.Column="3" Text="漢子"/></Grid></StackPanel>????????</Grid> </Window>

以上xaml就能實現視頻中的效果啦

如果喜歡,點個贊唄~您的點贊是我更新的動力~

總結

以上是生活随笔為你收集整理的WPF Grid添加边框的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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