Làm cách nào để mở rộng tất cả các nút TreeView trong WPF? Trong WinForms có một phương thức ExpandAll() thực hiện điều này.WPF TreeView: Phương thức ExpandAll() ở đâu
Trả lời
Lớp WPF TreeView không có phương thức ExpandAll. Vì vậy, bạn phải lặp qua các nút và thiết lập các thuộc tính IsExpanded của chúng thành true.
này có thể giúp
<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
với XAML Treeview phong cách bạn phải có một setter tài sản như những gì đã viết ở trên:
Trong file Cs, hãy viết các phương pháp như thế này, trong mẫu của tôi đã sử dụng nút và tên của treeview của tôi là myTV:
private void ExpandAll(ItemsControl items, bool expand)
{
foreach (object obj in items.Items)
{
ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
if (childControl != null)
{
ExpandAll(childControl, expand);
}
TreeViewItem item = childControl as TreeViewItem;
if (item != null)
item.IsExpanded = true;
}
}
private void btnExpandAll_Click(object sender, RoutedEventArgs e)
{
foreach (object item in this.myTV.Items)
{
TreeViewItem treeItem = this.myTV.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if (treeItem != null)
ExpandAll(treeItem, true);
treeItem.IsExpanded = true;
}
}
hy vọng nó có thể giúp bạn.
Ngoài nhận xét của người dùng2779123 và tôi biết điều này đã được trả lời từ lâu nhưng tôi cũng khuyên rằng mã của Pierre-Olivier Pignon đẩy treeItem.IsExpanded = true; không chỉ được di chuyển vào phạm vi của kiểm tra null mà bằng cách di chuyển nó đến quy trình ExpandAll vì nó có vẻ được viết theo định dạng cho phép mở rộng và thu hẹp cấu trúc cây và di chuyển đến đó sẽ thêm các nút gốc vào chức năng theo thiết kế.
Theo ví dụ dưới đây:
private void ExpandAll(ItemsControl items, bool expand)
{
items.IsExpanded = expand;
foreach (object obj in items.Items)
{
ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
if (childControl != null)
{
ExpandAll(childControl, expand);
}
TreeViewItem item = childControl as TreeViewItem;
if (item != null)
item.IsExpanded = true;
}
}
private void btnExpandAll_Click(object sender, RoutedEventArgs e)
{
foreach (object item in this.myTV.Items)
{
TreeViewItem treeItem = this.myTV.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if (treeItem != null)
ExpandAll(treeItem, true);
}
}
ItemsControl không có .IsExpanded, nhưng TreeViewItem có. – Tomas
Câu trả lời được cung cấp bởi @ Pierre-Olivier là một tốt nhất.
Cá nhân, tôi thích sử dụng ngăn xếp hơn là đệ quy trong các trường hợp như thế này, nơi bạn không biết có thể lồng sâu dữ liệu vào đâu. Thậm chí tốt hơn, nó có thể được cung cấp như là một chức năng mở rộng:
public static void ExpandAll(this TreeViewItem treeViewItem, bool isExpanded = true)
{
var stack = new Stack<TreeViewItem>(treeViewItem.Items.Cast<TreeViewItem>());
while(stack.Count > 0)
{
TreeViewItem item = stack.Pop();
foreach(var child in item.Items)
{
var childContainer = child as TreeViewItem;
if(childContainer == null)
{
childContainer = item.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
}
stack.Push(childContainer);
}
item.IsExpanded = isExpanded;
}
}
public static void CollapseAll(this TreeViewItem treeViewItem)
{
treeViewItem.ExpandAll(false);
}
/// <summary>
/// Collapse the TreeView.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void _collapseTreeView_button_Click(object sender, RoutedEventArgs e)
{
_ordersParentChild_TreeView.UpdateLayout();
if (_ordersParentChild_TreeView.Items.Count > 0)
{
var rootTreeViewItem = _ordersParentChild_TreeView.Items[0] as TreeViewItem;
if (rootTreeViewItem != null)
{
rootTreeViewItem.IsExpanded = false;
}
}
}
/// <summary>
/// Expand the TreeView.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void _expandTreeView_button_Click(object sender, RoutedEventArgs e)
{
_ordersParentChild_TreeView.UpdateLayout();
if(_ordersParentChild_TreeView.Items.Count > 0)
{
var rootTreeViewItem = _ordersParentChild_TreeView.Items[0] as TreeViewItem;
if (rootTreeViewItem != null)
{
rootTreeViewItem.ExpandSubtree();
}
}
}
}
Không hoạt động ... –
thử này
private void btnExpandAll_Click(object sender, RoutedEventArgs e)
{
foreach(var item in tvES.Items)
{
var tvi = item as TreeViewItem;
if (tvi != null)
tvi.ExpandSubtree();
}
}
- 1. byte IBuffer ở đâu []. Phương thức mở rộng AsBuffer?
- 2. Phương thức được lưu trữ trong bộ nhớ ở đâu?
- 3. Phương thức isAjax() trong Play Framework 2.0 ở đâu?
- 4. Tài liệu cho phương thức values () của Enum ở đâu?
- 5. Phương thức khởi động của Dịch vụ WCF ở đâu?
- 6. phương thức __doPostBack được gọi như thế nào? Phương thức gọi điện ở đâu?
- 7. WPF TreeView Binding
- 8. treeview Multibinding trong WPF
- 9. AutoExpand treeview trong WPF
- 10. WPF: Tùy chỉnh + - trong TreeView
- 11. Tài liệu chính thức cho các loại phương thức CVOpenGLESTexture ở đâu?
- 12. DTD podcast chính thức ở đâu?
- 13. Liên kết với một phương thức trong WPF?
- 14. Dữ liệu nhóm trong WPF treeview
- 15. Nhóm đối tượng con trong WPF TreeView
- 16. Phương thức COM ở Delphi
- 17. Hộp thoại bộ chọn tệp trong WPF ở đâu?
- 18. WPF TreeView rò rỉ mục đã chọn
- 19. Tiêu đề cột cho một TreeView WPF
- 20. WPF: Ảo hóa TreeView không hoạt động
- 21. WPF TreeView với nhiều lựa chọn
- 22. Phương thức has_header của Ruby tìm kiếm các tệp tiêu đề ở đâu?
- 23. Tôi đặt các phương thức chung vào ứng dụng MVC ở đâu
- 24. Tôi đặt phương thức trợ giúp cho chế độ xem ActionMailer ở đâu?
- 25. Trình biên dịch lưu trữ các phương thức cho các lớp C++ ở đâu?
- 26. Sử dụng class_getInstanceMethod - phương thức được triển khai trong hệ thống phân cấp lớp ở đâu?
- 27. Trong Ruby có cách nào để biết được phương thức được định nghĩa ở đâu?
- 28. Hằng số phương thức yêu cầu trong API Servlet ở đâu?
- 29. Phương thức getView() được sử dụng như thế nào và nó được gọi ở đâu?
- 30. Tôi đánh dấu biểu thức lambda ở đâu?
đơn giản và hiệu quả! Cảm ơn! – StinkyCat
Ok, nhưng giải pháp này là thời gian tải thay vì theo yêu cầu. – Ahmad