在 WPF 中动态添加网格和控件 [英] Adding Grid and Controls dynamically in WPF

查看:36
本文介绍了在 WPF 中动态添加网格和控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在做一个显示系统中硬盘使用情况的应用程序.为此,我想动态生成网格和控件(如进度条和标签)以显示用法.是否有任何 XAML 模板可用于此目的?

I am now doing an application that shows the HDD usage in the system. For that I want to generate the grid and the controls (like progress bar and labels) dynamically to show the usage. Is there any XAML template available for this purpose?

推荐答案

我使用以下代码在我的代码中动态生成控件

I use the folowing code to generate controls dynamically in my code

Label[] drivesLabel;
Label[] percentageLabel;
ProgressBar[] drivesProgress;
int drivesCount = 0;

private void DrawControls()
{
    try
    {
        // Getting drive list.
        List<DriveInfo> driveList = GetHardDiskDrives();
        drivesCount = driveList.Count;

        // Initializing new Grid.
        Grid drivesGrid = new Grid();
        drivesGrid.Children.Clear();
        DrivesBorder.Child = drivesGrid;

        // Adding Rows and Colums to Grid.
        RowDefinition[] rows = new RowDefinition[2*drivesCount + 1];
        ColumnDefinition[] columns = new ColumnDefinition[6];

        // Draw Rows.
        for (int i = 0; i < 2*drivesCount + 1; i++)
        {
            rows[i] = new RowDefinition();
            drivesGrid.RowDefinitions.Add(rows[i]);

            // Setting Row height.
            rows[i].Height = (0 == i % 2) ? new GridLength( 5 ): new GridLength( 25 );
        }
        // Draw Columns.
        for (int i = 0; i < 6; i++)
        {
            columns[i] = new ColumnDefinition();
            drivesGrid.ColumnDefinitions.Add(columns[i]);
            if (i % 2 == 0)
            {
                // Setting column width.
                columns[i].Width = new GridLength(5);
            }
        }

        // Setting column width.
        columns[1].Width = new GridLength(60);
        columns[3].Width = new GridLength(180);
        columns[5].Width = new GridLength(60);

        // Draw Labels to show drive letters.
        drivesLabel = new Label[drivesCount];

        // Draw Progress bar to show drive usage.
        drivesProgress = new ProgressBar[drivesCount];

        // Draw Labels to show drive usage.
        percentageLabel = new Label[drivesCount];

        // Adding Labels and Progressbars to Grid.
        for (int i = 0, j = 1; i < drivesCount; i++, j++)
        {
            // Initialize Labels to show drives.
            drivesLabel[i] = new Label();
            drivesLabel[i].Content = driveList[i].Name;
            drivesLabel[i].Foreground = Brushes.Navy;
            Grid.SetRow(drivesLabel[i], i + j);
            Grid.SetColumn(drivesLabel[i], 1);
            drivesGrid.Children.Add(drivesLabel[i]);

            // Initialize ProgressBar to show usage.
            drivesProgress[i] = new ProgressBar();
            drivesProgress[i].FlowDirection = FlowDirection.LeftToRight;
            drivesProgress[i].HorizontalAlignment = HorizontalAlignment.Center;
            drivesProgress[i].VerticalAlignment = VerticalAlignment.Center;
            drivesProgress[i].Orientation= Orientation.Horizontal;
            drivesProgress[i].Value = 0;
            drivesProgress[i].Maximum = 100;
            drivesProgress[i].Width = 180;
            drivesProgress[i].Height = 18;
            Grid.SetRow(drivesProgress[i], i + j);
            Grid.SetColumn(drivesProgress[i], 3);
            drivesGrid.Children.Add(drivesProgress[i]);

            // Initialize Labels  to show usage in percentage.
            percentageLabel[i] = new Label();
            percentageLabel[i].Content = "0 %";
            Grid.SetRow(percentageLabel[i], i + j);
            Grid.SetColumn(percentageLabel[i], 5);
            drivesGrid.Children.Add(percentageLabel[i]);

            // Setting window height.
            SetWindowHeight(30);
        }
    }
    catch(Exception Ex) {}
}

函数 GetHardDiskDrives() 和 SetWindowHeight() 是用户定义的函数.jpb 是获取硬盘并根据添加的新控件设置窗口高度.

The functions GetHardDiskDrives() and SetWindowHeight() are user defined functions. The jpb is to get harddrives and Set the window height according to the new controls added.

这篇关于在 WPF 中动态添加网格和控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆