DataGrid的主动行列表现

农民  金牌会员 | 2024-12-30 21:17:04 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 871|帖子 871|积分 2613

新建wpf页面DataGridAutoView

引用空间: xmlns:ga="clr-namespace:WPFDemoMVVM.Helpers"
  1.                 <Window x:
  2.                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.                                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.                                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.                                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.                                 xmlns:local="clr-namespace:WPFDemoMVVM.View"
  7.                                 xmlns:ga="clr-namespace:WPFDemoMVVM.Helpers"
  8.                                 mc:Ignorable="d"
  9.                                 Title="DataGridAutoView" Height="450" Width="800">
  10.                         <Grid>
  11.                                 <Grid ga:GridAssist.AutoRowColumn="_,2">
  12.                                         <Grid.Resources>
  13.                                                
  14.                                                
  15.                                         </Grid.Resources>
  16.                                         <Label Content="IP Address"></Label>
  17.                                         <TextBox Text="127.0.0.1"></TextBox>
  18.                                         <Label Content="Port"></Label>
  19.                                         <TextBox Text="8080"></TextBox>
  20.                                         <Label Content="TimeOut"></Label>
  21.                                         <TextBox Text="10000"></TextBox>
  22.                                 </Grid>
  23.                         </Grid>
  24.                 </Window>
复制代码
新建一个类GridAssist

在定名空间clr-namespace:WPFDemoMVVM.Helpers下,新建GridAssist类
  1.                 static class GridAssist
  2.                 {
  3.                         #region AutoRowColumn
  4.                         public static string GetAutoRowColumn(DependencyObject obj)
  5.                         {
  6.                                 return (string)obj.GetValue(AutoRowColumnProperty);
  7.                         }
  8.                         public static void SetAutoRowColumn(DependencyObject obj, string value)
  9.                         {
  10.                                 obj.SetValue(AutoRowColumnProperty, value);
  11.                         }
  12.                         /// <summary>
  13.                         /// 自动排列 Grid 容器中的所有控件
  14.                         /// </summary>
  15.                         /// <remarks>
  16.                         /// 值为一个逗号隔开的字符串,形如:<br/>
  17.                         /// - 2,3(2 行 3 列,列宽默认为 1*,行高为 Auto)<br/>
  18.                         /// - _,3(3 列,行数根据子控件而定)<br/>
  19.                         /// - 2,3,Auto(列的宽度为 Auto)<br/>
  20.                         /// Grid 中的所有控件将自动按照从左到右、从上到下的顺序进行排列<br/>
  21.                         /// 控件各自的 RowSpan 以及 ColumnSpan 也会被考虑
  22.                         /// </remarks>
  23.                         /// <example>
  24.                         ///
  25.                         /// <![CDATA[
  26.                         /// <Window xmlns:ap="clr-namespace:NemoDemo.AttachedProperties">
  27.                         ///     <Grid ap:GridHelper.AutoRowColumn="2,3">
  28.                         ///         <Button />                     // 1,1
  29.                         ///         <Label />                      // 1,2
  30.                         ///         <TextBox />                    // 1,3
  31.                         ///         <Button />                     // 2,1
  32.                         ///         <Label Grid.ColumnSpan="2" />  // 2,2-3
  33.                         ///     </Grid>
  34.                         /// </Window>
  35.                         /// ]]>
  36.                         ///
  37.                         /// </example>
  38.                         public static readonly DependencyProperty AutoRowColumnProperty = DependencyProperty.RegisterAttached(
  39.                                 "AutoRowColumn",
  40.                                 typeof(string),
  41.                                 typeof(GridAssist),
  42.                                 new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, OnAutoRowColumnChanged)
  43.                         );
  44.                         private static void OnAutoRowColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  45.                         {
  46.                                 if (!(d is Grid grid))
  47.                                         return;
  48.                                 var value = e.NewValue as string;
  49.                                 if (string.IsNullOrEmpty(value))
  50.                                 {
  51.                                         grid.RowDefinitions.Clear();
  52.                                         grid.ColumnDefinitions.Clear();
  53.                                         grid.Loaded -= OnGridLoaded;
  54.                                         return;
  55.                                 }
  56.                                 grid.Loaded += OnGridLoaded;
  57.                                 if (grid.IsLoaded)
  58.                                         OnGridLoaded(grid, null);
  59.                         }
  60.                         private static void OnGridLoaded(object sender, RoutedEventArgs e)
  61.                         {
  62.                                 var grid = sender as Grid;
  63.                                 // 列宽,默认为 Star,即平均分布
  64.                                 var width = new GridLength(1.0, GridUnitType.Star);
  65.                                 var split = GridAssist.GetAutoRowColumn(grid).Split(',');
  66.                                 var r = split[0] != "_" ? int.Parse(split[0]) : 1;
  67.                                 var c = int.Parse(split[1]);
  68.                                 // 如果有第三个参数且值为 auto,则宽度为 Auto
  69.                                 if (split.Length == 3 && split[2].Equals("auto", StringComparison.OrdinalIgnoreCase))
  70.                                         width = GridLength.Auto;
  71.                                 grid.RowDefinitions.Clear();
  72.                                 grid.ColumnDefinitions.Clear();
  73.                                 for (int i = 0; i < r; i++)
  74.                                         grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  75.                                 for (int i = 0; i < c; i++)
  76.                                         grid.ColumnDefinitions.Add(new ColumnDefinition { Width = width });
  77.                                 var cols = grid.ColumnDefinitions.Count;
  78.                                 var map = new List<bool[]>();
  79.                                 int x = 0,
  80.                                         y = 0;
  81.                                 foreach (UIElement item in grid.Children)
  82.                                 {
  83.                                         var rowSpan = Grid.GetRowSpan(item);
  84.                                         var colSpan = Grid.GetColumnSpan(item);
  85.                                         // 默认从上到下,从左到右,即任何时候控件的下方和右方都是空的
  86.                                         // 可能会出现中途有一个 RowSpan > 1 导致其左下方的右侧不为空的情况,暂不处理
  87.                                         // 同时默认当前的 (x, y) 位置是一个可用位置
  88.                                         // 当前控件占据的格子
  89.                                         for (int i = 0; i < rowSpan; i++)
  90.                                         {
  91.                                                 // 如果 RowDefinition 不够用,则自动添加
  92.                                                 if (map.Count <= y + i)
  93.                                                 {
  94.                                                         while (map.Count <= y + i)
  95.                                                         {
  96.                                                                 grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  97.                                                                 map.Add(new bool[cols]);
  98.                                                         }
  99.                                                 }
  100.                                                 map[y + i][x] = true;
  101.                                         }
  102.                                         for (int i = 0; i < colSpan; i++)
  103.                                         {
  104.                                                 if (x + i >= cols)
  105.                                                         break;
  106.                                                 map[y][x + i] = true;
  107.                                         }
  108.                                         Grid.SetRow(item, y);
  109.                                         Grid.SetColumn(item, x);
  110.                                         // 寻找下一个可用的格子
  111.                                         while (map[y][x])
  112.                                         {
  113.                                                 x++;
  114.                                                 if (x >= cols)
  115.                                                 {
  116.                                                         x = 0;
  117.                                                         y++;
  118.                                                         if (y >= map.Count)
  119.                                                         {
  120.                                                                 grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  121.                                                                 map.Add(new bool[cols]);
  122.                                                         }
  123.                                                 }
  124.                                         }
  125.                                 }
  126.                         }
  127.                         #endregion
  128.                 }
复制代码
wpf项目:https://gitee.com/chenshibao/wpfdemo

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表