网页制作网站首页设计,推广平台网站,高级采矿平台建立在小行星带,接网站开发做多少钱最近项目中使用到了Grid表格#xff0c;居然要加边框#xff0c;查了一下#xff0c;grid原生居然是不支持实线边框的。。最终我还是实现了效果#xff0c;看看吧#xff1a;左边是直接写的每行一个border,每列写一个border,这样在行列比较少的时候还行#xff0c;如果多… 最近项目中使用到了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:Classwpfcore.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:wpfcoremc:IgnorabledBackgroundLightBlueUseLayoutRoundingTrueTitleMainWindow Width820 Height340GridGrid.ColumnDefinitionsColumnDefinition/ColumnDefinition//Grid.ColumnDefinitionsStackPanel Margin30 VerticalAlignmentCenter HorizontalAlignmentCenterTextBlock Text直接写 Border的方式添加 FontSize18 Margin10 ForegroundGreen HorizontalAlignmentCenter/Grid Width300 Height150 Grid.ResourcesStyle TargetTypeTextBlockSetter PropertyHorizontalAlignment ValueCenter/Setter PropertyVerticalAlignment ValueCenter/Setter PropertyFontSize Value16/Setter PropertyFontFamily Value微软雅黑/
/StyleStyle TargetTypeBorder Setter PropertyBorderBrush ValueBlack/Setter PropertyBorderThickness Value1/
/Style/Grid.ResourcesGrid.ColumnDefinitionsColumnDefinition Width1.5*/ColumnDefinition Width1*/ColumnDefinition Width2*/ColumnDefinition Width1*//Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition /RowDefinition /RowDefinition /RowDefinition //Grid.RowDefinitionsBorder Grid.Row0 Grid.Column0 Grid.ColumnSpan4 Background#818591/Border Grid.Row1 Grid.Column0 Grid.ColumnSpan4 BorderThickness0 0 0 1/Border Grid.Row2 Grid.Column0 Grid.ColumnSpan4 BorderThickness0 0 0 1/Border Grid.Row3 Grid.Column0 Grid.ColumnSpan4 BorderThickness0 0 0 1/Border Grid.Row0 Grid.Column0 Grid.RowSpan4 /Border Grid.Row0 Grid.Column1 Grid.RowSpan4 BorderThickness0 0 1 0/Border Grid.Row0 Grid.Column2 Grid.RowSpan4 BorderThickness0 0 1 0/Border Grid.Row0 Grid.Column3 Grid.RowSpan4 BorderThickness0 0 1 0/TextBlock Grid.Row0 Grid.Column0 Text姓名/TextBlock Grid.Row0 Grid.Column1 Text年龄/TextBlock Grid.Row0 Grid.Column2 Text兴趣爱好/TextBlock Grid.Row0 Grid.Column3 Text性别/TextBlock Grid.Row1 Grid.Column0 TextWPF UI/TextBlock Grid.Row1 Grid.Column1 Text3M/TextBlock Grid.Row1 Grid.Column2 Text分享代码/TextBlock Grid.Row1 Grid.Column3 Text汉子//Grid/StackPanelStackPanel Grid.Column1 Margin30 VerticalAlignmentCenter HorizontalAlignmentCenterTextBlock Text使用附加属性添加 FontSize18 Margin10 ForegroundGreen HorizontalAlignmentCenter/Grid Width300 Height150 local:GridProperties.ShowBorderTrueGrid.ResourcesStyle TargetTypeTextBlockSetter PropertyHorizontalAlignment ValueCenter/Setter PropertyVerticalAlignment ValueCenter/Setter PropertyFontSize Value16/Setter PropertyFontFamily Value微软雅黑/
/StyleStyle TargetTypeBorderSetter PropertyBorderBrush ValueBlack/Setter PropertyBorderThickness Value1/
/Style/Grid.ResourcesGrid.ColumnDefinitionsColumnDefinition Width1.5*/ColumnDefinition Width1*/ColumnDefinition Width2*/ColumnDefinition Width1*//Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition /RowDefinition /RowDefinition /RowDefinition /RowDefinition //Grid.RowDefinitionsTextBlock Grid.Row0 Grid.Column0 Text姓名/TextBlock Grid.Row0 Grid.Column1 Text年龄/TextBlock Grid.Row0 Grid.Column2 Text兴趣爱好/TextBlock Grid.Row0 Grid.Column3 Text性别/TextBlock Grid.Row1 Grid.Column0 TextWPF UI/TextBlock Grid.Row1 Grid.Column1 Text3M/TextBlock Grid.Row1 Grid.Column2 Text分享代码/TextBlock Grid.Row1 Grid.Column3 Text汉子//Grid/StackPanel /Grid
/Window
以上xaml就能实现视频中的效果啦如果喜欢点个赞呗~您的点赞是我更新的动力~