• 如何自定义添加数据列表(数据单元格)


      在我们的项目中有时需要我们自己来添加数据单元格,而不是使用DataGrid或者是ListView等控件来填充数据,在我们下面的这个例子当中,我们只使用Grid和Label控件来实现类似DataGrid这种形式的单元格,下面我们通过一个例子来说明,这里只是贴出重点的代码形式。

    <Grid Grid.Row="2">
         <Grid.RowDefinitions>
           <RowDefinition Height="15*"></RowDefinition>
           <RowDefinition Height="100*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="60*"></ColumnDefinition>
           <ColumnDefinition Width="1*"></ColumnDefinition>
           <ColumnDefinition Width="40*"></ColumnDefinition>
           <ColumnDefinition Width="1*"></ColumnDefinition>
           <ColumnDefinition Width="40*"></ColumnDefinition>
           <ColumnDefinition Width="1*"></ColumnDefinition>
           <ColumnDefinition Width="60*"></ColumnDefinition>
       </Grid.ColumnDefinitions>
       <Label Grid.Column="0" Grid.Row="0" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14">治超站名称</Label>
       <Label Grid.Column="2" Grid.Row="0" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14">车牌号</Label>
       <Label Grid.Column="4" Grid.Row="0" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14">超限率</Label>
       <Label Grid.Column="6" Grid.Row="0" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14">违法时间</Label>

       <Grid Name="gData" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="7" Background="#657C9A" >
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
           </Grid.RowDefinitions>

      </Grid>

    <Rectangle Grid.Column="1" Grid.RowSpan="2" Fill="#829CBE"/>

    <Rectangle Grid.Column="3" Grid.RowSpan="2" Fill="#829CBE"/>

    <Rectangle Grid.Column="5" Grid.RowSpan="2" Fill="#829CBE"/>
    </Grid>
    </Grid>

      在我们定义的另外一个类中,我们会将数据添加到gData这个Grid中,这里我们先贴出子类的前台代码:

    <Grid x:Class="TrafficOverweight.ScreenViews.TrafficOverWeightListRow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="60*"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="40*"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="40*"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="60*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Name="lbl1" Grid.Column="0" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40"/>
        <Label Name="lbl2" Grid.Column="2" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40"/>
        <Label Name="lbl3" Grid.Column="4" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40"/>
        <Label Name="lbl4" Grid.Column="6" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40"/>
    </Grid>
    

      子类的后台代码如下:     

    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace TrafficOverweight.ScreenViews
    {
        /// <summary>
        /// TrafficOverWeightListRow.xaml 的交互逻辑
        /// </summary>
        public partial class TrafficOverWeightListRow : Grid
        {
            public TrafficOverWeightListRow(int pRow, string strStationName, string strLicensePlateNumber, double OverWeightRate, string strIllegalTime)
            {
                InitializeComponent();
    
                this.SetValue(Grid.RowProperty, pRow);
                lbl1.Content = strStationName;
                lbl2.Content = strLicensePlateNumber;
                lbl3.Content = OverWeightRate + "%";
                lbl4.Content = strIllegalTime;
                if (pRow % 2 == 0) { this.Background = new SolidColorBrush(Color.FromRgb(101, 124, 154)); }
                else { this.Background = new SolidColorBrush(Color.FromRgb(127, 150, 182)); }
                if (OverWeightRate >= 80) { this.Background = new SolidColorBrush(Color.FromRgb(153, 46, 46)); }
            }
        }
    }
    

    添加的时候通过下面的形式进行添加:

    /// <summary>
            /// 显示结果
            /// </summary>
            void ShowResult()
            {
                lblCount.Content = currentPage + "/" + maxPage;
    
                btnFirst.Tag = "1";
                btnFirst.Background = Brushes.Green;
                btnPre.Tag = "1";
                btnPre.Background = Brushes.Green;
                btnNext.Tag = "1";
                btnNext.Background = Brushes.Green;
                btnLast.Tag = "1";
                btnLast.Background = Brushes.Green;
    
                if (currentPage == 1)
                {
                    btnFirst.Tag = "0";
                    btnFirst.Background = Brushes.Gray;
                    btnPre.Tag = "0";
                    btnPre.Background = Brushes.Gray;
                }
                if (currentPage == maxPage)
                {
                    btnNext.Tag = "0";
                    btnNext.Background = Brushes.Gray;
                    btnLast.Tag = "0";
                    btnLast.Background = Brushes.Gray;
                }
    
                int numEnd = 0;
                if (currentPage == maxPage)
                {
                    numEnd = dtQueryResult.Rows.Count;
                }
                else
                {
                    numEnd = currentPage * pageRowsCount;
                }
    
                gData.Children.Clear();
                for (int i = (currentPage - 1) * pageRowsCount; i < numEnd; i++)
                {
                    TrafficOverWeightQueryRow tRow = new TrafficOverWeightQueryRow(i, dtQueryResult.Rows[i]["StationName"].ToString(), dtQueryResult.Rows[i]["LicensePlateNumber"].ToString(), Convert.ToDouble(dtQueryResult.Rows[i]["OverRate"]), dtQueryResult.Rows[i]["IllegalTime"].ToString(), this);
                    gData.Children.Add(tRow);
                }
            }
    

      这里代码的形式不是十分重要,重要的是这种通过类的封装来完成数据的加载,这种方法比较值得我们去学习。

  • 相关阅读:
    一条命令深度清理你的mac
    将以太坊封装为 ERC20
    golang subprocess tests
    go 笔记
    readme
    如何使用 channel
    修改vscode caipeiyu.writeCnblog ,简化博客发布
    thedao
    firefox 59 无法使用 pac 代理上网
    scrapy简单使用
  • 原文地址:https://www.cnblogs.com/seekdream/p/4845134.html
Copyright © 2020-2023  润新知