Tôi hiện đang thực hiện một ứng dụng hiển thị mức sử dụng HDD trong hệ thống. Đối với điều đó tôi muốn tạo lưới và các điều khiển (như thanh tiến trình và nhãn) động để hiển thị mức sử dụng. Có mẫu XAML nào có sẵn cho mục đích này không?Thêm lưới và điều khiển động trong WPF
Trả lời
tôi sử dụng mã folowing để tạo điều khiển tự động trong mã của tôi
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) {}
}
Các chức năng GetHardDiskDrives() và SetWindowHeight() là người dùng định nghĩa. Các jpb là để có được harddrives và Thiết lập chiều cao cửa sổ theo các điều khiển mới được thêm vào.
Tôi đã tìm thấy câu trả lời khác khó hiểu vì nhận xét nội tuyến ... thêm cộtDEFINITION (ví dụ) "vẽ" hoàn toàn NOTHING - điều đó rất gây nhầm lẫn cho người mới bắt đầu.
Ngoài ra: các hàng được chọn nhiều lần mặc dù chúng đã được chọn ... chỉ bổ sung thêm chi phí vô dụng. Điều này sẽ làm cho ứng dụng của bạn thực sự thực sự chậm nếu bạn đang sử dụng hàng trăm hàng.
Tương tự với cài đặt WindowHeight.
Dưới đây là một (hơi) giải pháp hiệu quả hơn cho hàng động & cột quản lý trong VB.NET: (sử dụng Dispatcher.BeginInvoke() thay vì Invoke() nếu bạn muốn chuyển sang chế biến không đồng bộ)
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. Thêm điều khiển động trong WPF MVVM
- 2. Điều khiển Bảng/Lưới nào trong WPF?
- 3. WPF: Cách tự động thêm các điều khiển trong cửa sổ WPF được tạo động
- 4. Bảng điều khiển bố cục Lưới WPF có chiều cao hàng được đặt thành "Tự động"
- 5. WPF Thêm thuộc tính tùy chỉnh trong điều khiển
- 6. Thêm điều khiển tự động trong FlowLayoutPanel
- 7. Thêm điều khiển vào Điều khiển người dùng động
- 8. Bảng điều khiển bọc WPF và cuộn
- 9. Điều khiển WPF trong Winforms
- 10. Phần tử bảng điều khiển động thêm và thanh cuộn
- 11. Thêm hàng/cột vào lưới động
- 12. cách thay đổi hàng lưới của điều khiển từ mã phía sau trong wpf
- 13. Thêm các điều khiển JavaFX2 động
- 14. Thêm thuộc tính vào điều khiển WPF tùy chỉnh?
- 15. WPF: Tại sao tất cả tình yêu dành cho điều khiển lưới?
- 16. WPF adorner với các điều khiển bên trong
- 17. Điều khiển lưới tương thích với .NET và Mono?
- 18. WPF: Cách mở ContextMenu từ tất cả các vùng của điều khiển Lưới
- 19. Điều khiển tổng hợp WPF
- 20. Có thể điều khiển WPF bằng điều khiển HwndHost không?
- 21. Xóa điều khiển TinyMCE và thêm lại
- 22. Thiết kế điều khiển bản đồ WPF
- 23. Điều khiển lưới MVC Grid so sánh
- 24. sự khác biệt giữa datagrid và lưới trong wpf
- 25. ASP.NET 2.0 thêm kiểu động vào trang trong điều khiển
- 26. Caliburn.Micro & Telerik WPF Điều khiển
- 27. Điều khiển động được thêm vào trong Asp.Net
- 28. Tự động thêm các điều khiển trong ASP.NET Repeater
- 29. Mẫu điều khiển DataFrid WPF
- 30. Điều khiển con trỏ WPF
Một nghìn cảm ơn Shining Annatar, đây chính là điều tôi đang tìm kiếm bạn đời :) –