Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2197
Cannot bind IsSelected property of XamDataTreeNode to a property in the ViewModel.
posted

Hello.

I am using Net Advantage 2012 volume 1 and I am using the XamDataTree in an MVVM project. The tree is in a UserControl and is bound to a collection of different types of ViewModels that all derive from a common NodeViewModel class that has a Selected property. What I'm looking to do is bind the IsSelected property of the XamDataTreeNode to the Selected property of the backing ViewModel. 

This works fine with the .NET TreeView. Here's the Xaml for the XamDataTree and the style targeting the XamDataTreeNode:

<Grid.Resources>
<Style x:Key="XamDataTreeNodeStyle" TargetType="{x:Type ig:XamDataTreeNode}">
<Setter Property="IsSelected" Value="{Binding Selected}"/>
</Style>
</Grid.Resources>

<ig:XamDataTree x:Name="FarmAndServerTree" Margin="5" ItemsSource="{Binding Children}"
NodeLineVisibility="Visible" ActiveNodeChanged="FarmAndServerTree_ActiveNodeChanged"
NodeStyle="{StaticResource XamDataTreeNodeStyle}">

Now, here's the Xaml for the .NET TreeView:

<Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding Selected}" />
<Setter Property="IsExpanded" Value="True" />
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Once" />
<EventSetter Event="TreeViewItem.MouseRightButtonDown" Handler="TreeViewItem_MouseRightButtonDown"/>
</Style>

<TreeView BorderThickness="0" ItemsSource="{Binding Children }" ItemContainerStyle="{StaticResource TreeViewItemStyle}">

When I run the application with the XamDataTree and set the NodeStyle to the XamDataTreeNodeStyle, the setter of the XamDataTreeNodeStyle throws this XamlParseException at run time:

"'XamDataTreeNode' type must derive from FrameworkElement or FrameworkContentElement."

This doesn't happen with the .NET TreeView; I can bind the IsSelected property of the TreeViewItem to the Selected property of the backing ViewModels with no issues. I found this thread which looks related:

http://es.infragistics.com/community/forums/t/72638.aspx

In that thread, it was said that you can't bind to the ActiveNode of the tree because it's not a Dependency property and they offered a workaround. I would assume that this is something similar because the XamDataTreeNode isn't a FrameworkElement. Is there any way to bind the IsSelected property of the nodes to a property in the backing ViewModel?

I can get around this by handling the ActiveNodeChanged event and then extracting the ViewModel from the e.NewActiveTreeNode property. Once I have that, I can manually set the Selected property of that ViewModel, but it would be much easier if I could hook this up in Xaml like you can with the .NET TreeView.

Thanks in advance for your help.