我现在正在做一个应用程序,显示系统中的HDD使用情况。为此,我想动态生成网格和控件(如进度条和标签)以显示使用情况。有没有可用于此目的的XAML模板?在WPF中动态添加网格和控件
16
A
回答
16
我使用如下因素代码在我的代码动态地生成控制
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将获得硬盘驱动器并根据添加的新控件设置窗口高度。
1
我发现另一个答案令人困惑,因为内嵌注释...添加一个columnDEFINITION(例如)“绘制”绝对没有 - 这对初学者来说非常具有误导性。
此外:即使它们已被选中,行也会被重复选择......这只会增加无用的开销。如果你使用几百行,这会让你的应用真的很慢。
与设置WindowHeight相同。
这里有一个(有点)更高效地为动态记录&列管理VB.NET解决方案: (使用Dispatcher.BeginInvoke()而不是调用(),如果你想切换到异步处理)
Private Delegate Sub MyDelegate3(ByVal iByte As Byte)
Private Delegate Function MyDelegate4() As Byte
Public Property GridColumns As Byte
Get
Dim del As New MyDelegate4(AddressOf GetColumns)
Return grid.Dispatcher.Invoke(del)
End Get
Set(ByVal value As Byte)
Dim del As MyDelegate3
If GridColumns > 0 Then
Dim diff As SByte = GridColumns - value
If diff > 0 Then 'Spalten abziehen
del = New MyDelegate3(AddressOf RemColDefs)
grid.Dispatcher.Invoke(del, diff)
Else 'Spalten hinzufügen
del = New MyDelegate3(AddressOf AddColDefs)
grid.Dispatcher.Invoke(del, Math.Abs(diff))
End If
Else
del = New MyDelegate3(AddressOf AddColDefs)
grid.Dispatcher.Invoke(del, value)
End If
End Set
End Property
Public Property GridRows As Byte
Get
Dim del As New MyDelegate4(AddressOf GetRows)
Return grid.Dispatcher.Invoke(del)
End Get
Set(value As Byte)
Dim del As MyDelegate3
If GridRows > 0 Then
Dim diff As SByte = GridRows - value
If diff > 0 Then 'Zeilen abziehen
del = New MyDelegate3(AddressOf RemRowDefs)
grid.Dispatcher.Invoke(del, diff)
Else 'Spalten hinzufügen
del = New MyDelegate3(AddressOf AddRowDefs)
grid.Dispatcher.Invoke(del, Math.Abs(diff))
End If
Else
del = New MyDelegate3(AddressOf AddRowDefs)
grid.Dispatcher.Invoke(del, value)
End If
End Set
End Property
Private Function GetRows() As Byte
Return grid.RowDefinitions.Count
End Function
Private Function GetColumns() As Byte
Return grid.ColumnDefinitions.Count
End Function
Private Sub AddRowDefs(ByVal iRows As Byte)
For r As Byte = 1 To iRows
Dim rowDef As New RowDefinition
rowDef.Height = GridLength.Auto
grid.RowDefinitions.Add(rowDef)
Next
End Sub
Private Sub RemRowDefs(ByVal iRows As Byte)
For r As Byte = 1 To iRows
If grid.RowDefinitions.Count > 0 Then
grid.RowDefinitions.Remove(grid.RowDefinitions(0))
End If
Next
End Sub
Private Sub AddColDefs(ByVal iCols As Byte)
For r As Byte = 1 To iCols
Dim colDef As New ColumnDefinition
colDef.Width = GridLength.Auto
grid.ColumnDefinitions.Add(colDef)
Next
End Sub
Private Sub RemColDefs(ByVal iCols As Byte)
For r As Byte = 1 To iCols
If grid.ColumnDefinitions.Count > 0 Then
grid.ColumnDefinitions.Remove(grid.ColumnDefinitions(0))
End If
Next
End Sub
相关问题
- 1. 在WPF MVVM中动态添加控件
- 2. WPF动态添加图像控件到动态构建的网格
- 3. WPF动态添加控件到窗口
- 4. 在wpf中动态添加面板到网格中
- 5. WPF:如何在动态创建的WPF窗口中动态添加控件
- 6. 在Silverlight中动态添加/删除网格中的行和控件
- 7. 在窗口/网格中添加WPF用户控件QT方式
- 8. 使用WPF/XAML添加动态控件到表格
- 9. 网格控制的动态列WPF
- 10. 动态添加OCX控件到网页
- 11. 动态添加网页用户控件
- 12. 在WPF控件之间添加空格
- 13. 动态添加网格UWP
- 14. 无法将行动态添加到WPF中的网格
- 15. 将数据添加到WPF中的动态数据网格
- 16. WPF动态添加控件和绑定的问题
- 17. 在WPF中没有动态控件的布局和格式化
- 18. WPF中的动态网格测量 - 渲染控件
- 19. 动态添加面板或标签控件到数据网格
- 20. Silvelight:如何动态添加网格行之间的控件?
- 21. WPF绑定动态添加控制
- 22. 如何加速向WPF网格添加控件?
- 23. 在ASP.NET Repeater中动态添加控件
- 24. 动态地在gridview中添加控件
- 25. 在C#中动态添加控件
- 26. 在网格视图中动态添加网格项
- 27. 添加在网格视图行动态
- 28. AutoScaleMode.Font和动态添加的控件
- 29. 动态添加控件。 Asp.Net和VB
- 30. 动态添加用户控件和ViewState
千谢谢Shining Annatar,这正是我期待的伴侣:) – 2010-05-04 20:47:04