TreeViewNode, How to give value

Viewed 55

I'm trying to assign a value to My TreeView with the name "tbYear", this value I want to assign comes from My API. I'm getting the value correctly, I just have to be able to realize this piece of code. I'm using UWP, C#.

My TreeView in file.xaml

<muxc:TreeView x:Name="tbYear">
            <muxc:TreeView.RootNodes>
                <muxc:TreeViewNode Content="Year" IsExpanded="False">
                    <muxc:TreeViewNode.Children>
                        <muxc:TreeViewNode Content="1º"/>
                        <muxc:TreeViewNode Content="2º"/>
                    </muxc:TreeViewNode.Children>
                </muxc:TreeViewNode>
            </muxc:TreeView.RootNodes>
</muxc:TreeView>

My file.xaml.cs

private async void GetData
{
        srcResults = await apiclient.GetValues();
        tbYear.SelectedItem = srcResults.Year;
}

But i get the error: "Cannot implicityl convert type 'string' to 'Microsoft.UI.Xaml.Controls.TreeViewNode'"

How can I solve it?

1 Answers

Here is an example of you could select the "1º" TreeViewNode using the SelectedNode property:

tbYear.SelectedNode = 
    tbYear.RootNodes[0].Children.FirstOrDefault(x => x.Content?.ToString() == "1º");

Should work as-is assuming that srcResults.Year is either "1º" or "2º". Replace the hardcoded string with srcResults.Year.

Related