BoldDesk®Customer service software offering ticketing, live chat, and omnichannel support, starting at $49/mo. for 10 agents. Try it for free.
I have a user control that includes a Syncfusion TreeView. Following the Syncfusion online examples for a WinUI TreeView (https://help.syncfusion.com/winui/treeview/contextflyout), the control has a data context:
<UserControl.DataContext>
<viewmodel:MyViewModel />
</UserControl.DataContext>
The exact syncfusion example XAML code for attaching to a command in the data context leads to a compile error in my code for the data context. Because of the data context noted above, however, I only needed to include the name of the command in MyViewModel, which is ExpandCommand. For Example:
<MenuFlyoutItem
x:Name="Expand"
Command="{Binding ExpandCommand}"
CommandParameter="{Binding}"
Text="Expand" />
In the viewmodel, I have:
public MyViewModel()
{
ExpandCommand = new DelegateCommand<object>(ExecuteExpandCommand, CanExecuteExpandCommand);
}
public ICommand ExpandCommand { get; set; }
private bool CanExecuteExpandCommand(object obj)
{
if (obj is not TreeViewItemContextFlyoutInfo itemContextFlyoutInfo)
return false;
if (itemContextFlyoutInfo.Node.HasChildNodes)
return true;
return false;
}
private static void ExecuteExpandCommand(object obj)
{
if (obj is not TreeViewItemContextFlyoutInfo itemContextFlyoutInfo)
return;
itemContextFlyoutInfo.TreeView.ExpandNode(itemContextFlyoutInfo.Node);
}
This all seems to coincide with the Syncfusion examples, but for some reason, the command is not triggering at all. I've tried using breakpoints, but nothing. What might be missing?
Thanks for the help.
<UserControl.Resources> |
Attachment: SfTreeView_Net60_508f1597.zip
I ended up using IRelayCommand from the CommunityToolkit.Mvvm package and x:Bind for the XAML binding, which works in my case. My ViewModel is defined in the code behind, but it could be bound in the XAML.
In XAML:
<MenuFlyoutItem
x:Name="Expand"
Command="{x:Bind ViewModel.ExpandNodeCommand}"
CommandParameter="{Binding}"
Text="Expand" />
In the ViewModel:
public IRelayCommand ExpandNodeCommand { get; }
public WBSViewModel()
{
...
ExpandNodeCommand = new RelayCommand<object>(ExecuteExpandCommand, CanExecuteExpandOrCollapseCommand);
}
private bool CanExecuteExpandOrCollapseCommand(object obj) {...}
private static void ExecuteExpandCommand(object obj) {...}