private void TreeViewItem_Expanded(object sender, RoutedEventArgs e) { try { TreeViewItem item = e.Source as TreeViewItem; if ((item.Items.Count == 1) && (item.Items[0] is int)) { TreeViewItemTag tag = (TreeViewItemTag)item.Tag; IResultTreeItem resultTreeItem = tag.ResultTreeItem; int level = tag.Level; TreeViewItem lastItem = null; item.Items.Clear(); foreach (var child in resultTreeItem.Children.OrderBy(s => s.Name.StartsWith("[")).ThenBy(s => s.Name)) { item.Items.Add(lastItem = CreateTreeItem(child, level + 1)); } // Check if we need to fix empty list item width if (lastItem != null && double.IsNaN(emptyListItem.Width)) { item.Dispatcher.BeginInvoke(new Action(() => { emptyListItem.Width = item.ActualWidth - lastItem.ActualWidth; }), System.Windows.Threading.DispatcherPriority.Background); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private UIElement Visualize(object obj) { // Create top level table grid Grid tableGrid = new Grid(); dispatcher = tableGrid.Dispatcher; Grid.SetIsSharedSizeScope(tableGrid, true); tableGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto), }); // Create table header TreeViewItem header = new TreeViewItem(); Grid headerGrid = CreateTreeItemGrid(0); TextBlock name = new TextBlock(); name.Text = "Name"; name.FontWeight = FontWeights.Bold; Grid.SetColumn(name, NameColumnIndex); headerGrid.Children.Add(name); TextBlock value = new TextBlock(); value.Text = "Value"; value.FontWeight = FontWeights.Bold; Grid.SetColumn(value, ValueColumnIndex); headerGrid.Children.Add(value); TextBlock type = new TextBlock(); type.Text = "Type"; type.FontWeight = FontWeights.Bold; Grid.SetColumn(type, TypeColumnIndex); headerGrid.Children.Add(type); emptyListItem = new TreeViewItem(); emptyListItem.Padding = new Thickness(0); emptyListItem.Focusable = false; Grid.SetColumn(emptyListItem, 1); emptyListItem.Focusable = false; headerGrid.Children.Add(emptyListItem); header.Focusable = false; header.Header = headerGrid; // Create table tree TreeView tree = new TreeView(); IResultTreeItem resultTreeItem = ResultTreeItem.Create(obj, obj.GetType(), "result", null, this); tree.PreviewKeyDown += Tree_PreviewKeyDown; tree.Items.Add(header); tree.Items.Add(CreateTreeItem(resultTreeItem, 0)); ((TreeViewItem)tree.Items[1]).IsSelected = true; tableGrid.Children.Add(tree); return(tableGrid); }
private TreeViewItem CreateTreeItem(IResultTreeItem resultTreeItem, int level) { TreeViewItem item = new TreeViewItem(); Grid grid = CreateTreeItemGrid(level); UIElement name = CreateTextWithIcon(resultTreeItem.Name, resultTreeItem.Image); grid.Children.Add(name); object itemValue = resultTreeItem.Value; if (itemValue is UIElement) { Grid.SetColumn((UIElement)itemValue, ValueColumnIndex); grid.Children.Add((UIElement)itemValue); } else { TextBlock value = new TextBlock(); value.Text = itemValue.ToString(); Grid.SetColumn(value, ValueColumnIndex); grid.Children.Add(value); } TextBlock type = new TextBlock(); type.Text = resultTreeItem.Type; Grid.SetColumn(type, TypeColumnIndex); grid.Children.Add(type); item.Header = grid; item.Tag = new TreeViewItemTag() { ResultTreeItem = resultTreeItem, Level = level, }; if (resultTreeItem.Children.Any()) { item.Items.Add(0); } item.Expanded += TreeViewItem_Expanded; return(item); }
private TreeViewItem CreateTreeItem(IResultTreeItem resultTreeItem, int level) { TreeViewItem item = new TreeViewItem(); Grid grid = CreateTreeItemGrid(level); UIElement name = CreateTextWithIcon(resultTreeItem.Name, resultTreeItem.Image); grid.Children.Add(name); object itemValue = resultTreeItem.Value; if (itemValue is UIElement) { Grid.SetColumn((UIElement)itemValue, ValueColumnIndex); grid.Children.Add((UIElement)itemValue); } else { TextBlock value = new TextBlock(); value.Text = resultTreeItem.ValueString; Grid.SetColumn(value, ValueColumnIndex); grid.Children.Add(value); } TextBlock type = new TextBlock(); type.Text = resultTreeItem.Type; Grid.SetColumn(type, TypeColumnIndex); grid.Children.Add(type); item.Header = grid; item.Tag = new TreeViewItemTag() { ResultTreeItem = resultTreeItem, Level = level, }; if (resultTreeItem.Children.Any()) item.Items.Add(0); item.Expanded += TreeViewItem_Expanded; return item; }