Hello @BitSmithy
Welcome to Microsoft Q&A!
Referring to the official example, you can see that class Item is reused in the sample. we need to design a class for treeview that calls itself repeatedly. Otherwise the code will not run.
public class TreeViewInfo
{
public string Name { get; set; }
public string Desc { get; set; }
public bool isMaster { get; set; }
public List<TreeViewInfo> Children { get; set; }
}
So we can't detect which class it is in SelectTemplateCore, even if I set a bool value, it can't be detected. This is by design, your requirement cannot be implemented in UWP Treeview.
The following is my test code. This version is the closest to your needs, but the content of the button cannot be changed according to the role.
<Grid>
<TreeView x:Name="treeView" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:TreeViewInfo">
<TreeViewItem ItemsSource="{x:Bind Children}" IsExpanded="True">
<StackPanel Orientation="Horizontal">
<Button Content="CC"></Button>
<Button Content="DD"></Button>
<TreeViewItem Content="{x:Bind Name}"/>
<TreeViewItem Content="{x:Bind Desc}"/>
</StackPanel>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
LoadData();
}
private void LoadData()
{
var infos = new List<TreeViewInfo>
{
new TreeViewInfo
{
Name = "Parent 1", Desc = "AAAAAAAAA", isMaster=true,
Children = new List<TreeViewInfo>
{
new TreeViewInfo { Name = "Child 1", isMaster=false, },
new TreeViewInfo { Name = "Child 2", isMaster=false }
}
},
new TreeViewInfo
{
Name = "Parent 2", Desc = "BBBBBBBBB", isMaster=true,
Children = new List<TreeViewInfo>
{
new TreeViewInfo { Name = "Child 3" , isMaster=false},
new TreeViewInfo { Name = "Child 4", isMaster=false }
}
}
};
treeView.ItemsSource = infos;
}
}
public class TreeViewInfo
{
public string Name { get; set; }
public string Desc { get; set; }
public bool isMaster { get; set; }
public List<TreeViewInfo> Children { get; set; }
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.