This is a migrated thread and some comments may be shown as answers.

TreeViewItem Command binding

2 Answers 580 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Remus
Top achievements
Rank 1
Veteran
Remus asked on 17 Mar 2021, 10:55 AM

Hello,

I have a treeView and I want to bind each item command, command parameter and IsEnabled properties. If I bind just the IsEnabled property it works, but if I add the command binding, the command works, but IsEnabled binding breaks.

I use the following code:

<telerik:RadTreeView ItemsSource="{Binding TreeViewModel.Categories}"
                                         Grid.Row="0">
                        <telerik:RadTreeView.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding SubCategories}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock VerticalAlignment="Center" Text="{Binding Name}" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </telerik:RadTreeView.ItemTemplate>
                        <telerik:RadTreeView.ItemContainerStyle>
                            <Style TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
                                <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
                                <Setter Property="Command" Value="{Binding Path=DataContext.GoToTabCommand, RelativeSource={RelativeSource AncestorType=telerik:RadWindow}}" />
                                <Setter Property="CommandParameter" Value="{Binding Name}" />
                            </Style>
                        </telerik:RadTreeView.ItemContainerStyle>
                    </telerik:RadTreeView>

 

 

How can I make both bindings to work?

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Petrov
Telerik team
answered on 19 Mar 2021, 04:11 PM

Hi Ramus,

Thank you for the provided XAML, it truly helps.

When you bind the Command property of a RadTreeViewItem we internally set the IsEnabled property whenever the CanExecute value of the command changes.

So to accommodate this into your solution my suggestion is to bind the CommandParameter to the actual underlying view model like so:

<telerik:RadTreeView.ItemContainerStyle>
    <Style TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
        <Setter Property="Command" Value="{Binding Path=DataContext.GoToTabCommand, RelativeSource={RelativeSource AncestorType=telerik:RadWindow}}" />
        <Setter Property="CommandParameter" Value="{Binding}" />
    </Style>
</telerik:RadTreeView.ItemContainerStyle>

And then inside your command return the value of the view model's IsEnabled property.

public class GoToTabCommand : ICommand
{
	public bool CanExecute(object parameter)
	{
               Category category = parameter as Category;

                return category.IsEnabled;
	}

	public void Execute(object parameter)
	{
	}

	public event EventHandler CanExecuteChanged;
}

I hope this is useful. Should you have further questions, I would be glad to help.

Regards,
Ivan Petrov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Remus
Top achievements
Rank 1
Veteran
answered on 22 Mar 2021, 07:40 AM

Thanks Ivan,

This solution works for me!

Tags
TreeView
Asked by
Remus
Top achievements
Rank 1
Veteran
Answers by
Ivan Petrov
Telerik team
Remus
Top achievements
Rank 1
Veteran
Share this question
or