C
C#2y ago
JimmahDean

❔ UWP TreeView is not stretching to fill space

Title pretty much explains it all: I have a TreeView that's not stretching and stays 0px x 0px if there's no data. It's not the end of the world I guess, but I would really like it to stretch to fill if possible. What am I missing?
2 Replies
JimmahDean
JimmahDeanOP2y ago
Secondary problem: I can't figure out how to get it to display nested objects. I have the Plan, which should display its name, then a list of Base Items that are used in the plan and then a list of options, which include more items to add to the plan. All of the info is stored in tuples, and the microsoft documentation does not have information on how to bind to tuples, nor does it have examples with multiple levels of nesting that I could find
<Page.Resources>
<DataTemplate x:Key="SubPlanTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding planID}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="OptionTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding optionID}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Page.Resources>
<DataTemplate x:Key="SubPlanTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding planID}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="OptionTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding optionID}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<TreeView x:Name="tree_PlansView" RelativePanel.Below="tbx_PlansLabel" Margin="16,4,0,8" CornerRadius="16" BorderThickness="2" BorderBrush="Black" ItemsSource="{x:Bind SubdivisionPlans}" MinHeight="300" MinWidth="600">
<TreeView.ItemContainerStyle>
<StaticResource ResourceKey="TreeViewItemStyle" />
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemTemplateSelector>
<local:SubPlanOptionTemplateSelector
SubPlanTemplate="{StaticResource SubPlanTemplate}"
OptionTemplate="{StaticResource OptionTemplate}" />
</TreeView.ItemTemplateSelector>
</TreeView>
<TreeView x:Name="tree_PlansView" RelativePanel.Below="tbx_PlansLabel" Margin="16,4,0,8" CornerRadius="16" BorderThickness="2" BorderBrush="Black" ItemsSource="{x:Bind SubdivisionPlans}" MinHeight="300" MinWidth="600">
<TreeView.ItemContainerStyle>
<StaticResource ResourceKey="TreeViewItemStyle" />
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemTemplateSelector>
<local:SubPlanOptionTemplateSelector
SubPlanTemplate="{StaticResource SubPlanTemplate}"
OptionTemplate="{StaticResource OptionTemplate}" />
</TreeView.ItemTemplateSelector>
</TreeView>
public class SubPlan
{
public int planID;
public string Name;
public ObservableCollection<(int, string)> BaseItems;
public ObservableCollection<Option> Options;
}
public class Option
{
public int optionID;
public string Name;
public ObservableCollection<(int, string)> AddItems;
public ObservableCollection<(int, string)> RemoveItems;
}
public class SubPlanOptionTemplateSelector : DataTemplateSelector
{
public DataTemplate SubPlanTemplate { get; set; }
public DataTemplate OptionTemplate { get; set; }

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is SubPlan)
{
return SubPlanTemplate;
}
else if (item is Option)
{
return OptionTemplate;
}
return base.SelectTemplateCore(item, container);
}
}
public class SubPlan
{
public int planID;
public string Name;
public ObservableCollection<(int, string)> BaseItems;
public ObservableCollection<Option> Options;
}
public class Option
{
public int optionID;
public string Name;
public ObservableCollection<(int, string)> AddItems;
public ObservableCollection<(int, string)> RemoveItems;
}
public class SubPlanOptionTemplateSelector : DataTemplateSelector
{
public DataTemplate SubPlanTemplate { get; set; }
public DataTemplate OptionTemplate { get; set; }

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is SubPlan)
{
return SubPlanTemplate;
}
else if (item is Option)
{
return OptionTemplate;
}
return base.SelectTemplateCore(item, container);
}
}
public sealed partial class SubdivisionsPage : Page
{
ObservableCollection<SubPlan> SubdivisionPlans = new();
public sealed partial class SubdivisionsPage : Page
{
ObservableCollection<SubPlan> SubdivisionPlans = new();
I can post the code populating SubdivisionPlans if it's necessary, but the collection gets populated, it just doesn't display at all full disclosure: the treeview code was written by chatgpt, i tried a few things on my own and got nowhere
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server