-
I try to implement TreeView in my app, but have serious problems: can't see all tree levels, can't expand, can't click any items. Can anyone help me, please? I have a View like this with TreeView component
Item template:
Method for TreeItems creation
So, we have at least 2 levels of tree, but in app we have only top level. Result is on the video: KaliberURP_TreeView.mp4Additional info:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
My guess is that it's not a C# Markup problem. Are you able to share source code of a repro of just cod trying to create the TreeView in C# with some items? Also share more details about the exception you get when you click on an item? Thank you! |
Beta Was this translation helpful? Give feedback.
-
Hi @mpa74! Taking a look at your markup code for the bindings of the ItemTemplate and ItemsSource of the TreeView, I think the wrong extension methods are being used here. You can take a look at the docs here on Templates and Bindings in C# Markup. Following that doc I created some sample code that generates the following TreeView structure: ![]() Here's the markup code that creates that: namespace UnoApp29;
public sealed partial class MainPage : Page
{
public record Item(string Name, Item[] Children)
{
public Item() : this(string.Empty, [])
{
}
}
private Item[] _items =
[
new Item()
{
Name = "Flavors",
Children =
[
new Item() { Name = "Vanilla" },
new Item() { Name = "Strawberry" },
new Item() { Name = "Chocolate" }
]
},
new Item()
{
Name = "Toppings",
Children =
[
new Item()
{
Name = "Candy",
Children =
[
new Item() { Name = "Chocolate" },
new Item() { Name = "Mint" },
new Item() { Name = "Sprinkles" }
]
},
new Item()
{
Name = "Fruits",
Children =
[
new Item() { Name = "Mango" },
new Item() { Name = "Peach" },
new Item() { Name = "Kiwi" }
]
},
new Item()
{
Name = "Berries",
Children =
[
new Item() { Name = "Strawberry" },
new Item() { Name = "Blueberry" },
new Item() { Name = "Blackberry" }
]
}
]
}
];
public MainPage()
{
this
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Children(
new TreeView()
.ItemsSource(_items)
.ItemTemplate<Item>(item =>
new TreeViewItem()
.Content(() => item.Name)
.ItemsSource(() => item.Children))
));
}
} |
Beta Was this translation helpful? Give feedback.
Hi @mpa74!
Taking a look at your markup code for the bindings of the ItemTemplate and ItemsSource of the TreeView, I think the wrong extension methods are being used here. You can take a look at the docs here on Templates and Bindings in C# Markup. Following that doc I created some sample code that generates the following TreeView structure:
Here's the markup code that creates that: