Binding of SelectedItem is not working? + how to make refresh of nodes?

Hello,

I have a SfTreeView:

<Syncfusion:SfTreeView Grid.Row="1" HorizontalAlignment="Left" Width="369" Margin="4,2,0,2"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ShowLines="True" ShowRootLines="True" UseLayoutRounding="False"
        ItemsSource="{Binding SelectedRepository.Nodes}"
        ChildPropertyName="ChildNodes"
        LoadOnDemandCommand="{Binding ReadChildNodesCommand}"
        SelectionMode="Single"
        SelectedItem="{Binding SelectedNode}">
    <Syncfusion:SfTreeView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </Syncfusion:SfTreeView.ItemTemplate>
</Syncfusion:SfTreeView>

The SelectedNode is in ViewModel defined as follows:

private object? selectedNode = null;
public object? SelectedNode { get => selectedNode; set => Set(nameof(SelectedNode), ref selectedNode, value); }

The SfTreeView is showing all data, I can select any item, but the value of SelectedNode is not changing. What am I doing wrong?

My second question is related to refreshing data: If I got it well, SelectedItem points to the data binded with the node. This node has also childnodes. If I refresh the data, how should I populate the items?

Cheers.


1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team January 10, 2022 04:14 PM UTC

Hi Slávek,

Please find answer for your queries below 
Queries 
Solutions 
 
The SfTreeView is showing all data, I can select any item, but the value of SelectedNode is not changing. What am I doing wrong? 
 
 
Your requirement can be achieved by enabling the UpdateSourceTrigger property as PropertyChanged and set Mode as TwoWay for SelectedItem property in SfTreeView. Please refer the below code snippet,

 
<syncfusion:SfTreeView Grid.Row="1"  
                               HorizontalAlignment="Left"  
                               Width="369"  
                               Margin="4,2,0,2"              
                               ScrollViewer.HorizontalScrollBarVisibility="Auto"         
                               ShowLines="True"  
                               ShowRootLines="True"  
                               UseLayoutRounding="False" 
                               x:Name="sfTreeView"             
                               ItemsSource="{Binding Menu}" 
                               LoadOnDemandCommand="{Binding TreeViewOnDemandCommand}"             
                               SelectionMode="Single"             
                               SelectedItem="{Binding SelectedNode,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
            <syncfusion:SfTreeView.ItemTemplate> 
                <DataTemplate> 
                    <TextBlock Text="{Binding ItemName}"/> 
                </DataTemplate> 
            </syncfusion:SfTreeView.ItemTemplate> 
</syncfusion:SfTreeView> 

Sample Link: https://www.syncfusion.com/downloads/support/forum/171805/ze/LoadOnDemand41424265

For more information related to Selection, please refer the below user guide documentation link,

UG Link:
https://help.syncfusion.com/wpf/treeview/selection

 
 
My second question is related to refreshing data: If I got it well, SelectedItem points to the data binded with the node. This node has also childnodes. If I refresh the data, how should I populate the items? 
 
 
You can load child items for the node in Execute method of LoadOnDemandCommand. Execute method will get called when user expands the tree node. For more information related to On-demand loading of child items, please refer the below user guide documentation link, 
 

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon