Articles in this section
Category / Section

How to bind node data as CommandParameter for TreeView ItemTemplate content in Xamarin.Forms (SfTreeView)

1 min read

You can bind the node item to CommandParameter in Xamarin.Forms SfTreeview by binding with dot.

 

C#

ItemInfoCommand defined to get the node data in the ViewModel.

public class FileManagerViewModel
{
    public Command<object> NodeCommand { get; set; }
    public FileManagerViewModel()
    { 
        NodeCommand = new Command<object>(OnNodeExecuted);
    }
    public void OnNodeExecuted(object data)
    {
        App.Current.MainPage.DisplayAlert("Alert", (data as FileManager).ItemName.ToString() , "OK");
    }
}

XAML

Binding (.) dot to CommandParameter of ViewModel command and will get the node data in the handler.

<Grid Grid.Column=”1”
        RowSpacing="1"
        Padding="1,0,0,0"
        VerticalOptions="Center">
    <Label LineBreakMode="NoWrap" TextColor="Black"
            Text="{Binding ItemName}"
            VerticalTextAlignment="Center">
    </Label>
    <Button Grid.Column="1" HorizontalOptions="End" Text="Node Data" CommandParameter="{Binding . }" 
    Command="{Binding Path=BindingContext.NodeCommand, Source={x:Reference treeView}}"/>
</Grid>

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied