码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-28 23:45:01      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

  在我们的项目中有时需要我们自己来添加数据单元格,而不是使用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);
            }
        }

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

 

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

标签:

原文地址:http://www.cnblogs.com/seekdream/p/4845134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!