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

Selected Item Not Working in MVVM Binding if Tree Collapsed

3 Answers 159 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 02 May 2013, 10:47 AM
I'm using the RadTreeListView v4.0.30319 to display my hierarchical data set. I know this is an older version but breaking changes in the newer version of the GridView (the ones that cause data duplication on refreshing) mean I can't upgrade.

My problem is this.

My TreeListView has a two-way binding on the SelectedItem property. If I add a new item to the underlying hierarchical data, it appears in the grid and is selected how I would expect, but only if the branch of the tree I am adding to is expanded. If I add a new item to a currently collapsed group, the proeprty I am binding to (SelectedLocation in my case) updates as expected but the parent branch remains collapsed and the tree's rendering does not change. This makes it look like the previously selected item is still selected.
Is there a solution to this that is pure MVVM (ie. no code behind the window)?

I have tried setting the AutoExpandGroups to true to see if this helped. However, there appears to be a bug in this. In my data I have two root objects, each with several children, grandchildren, etc. When I have AutoExpandGroups set to true, only the first root and the first child are displayed. Everything else only appears if you collapse and re-expand nodes. I'm assuming I'm stuck with this because of not being able to upgrade to the later version of the controls due to the breaking change in the GridView.

My current TreeListView's XAML is:

    <Grid Visibility="{Binding .,Converter={StaticResource PermissionCodeConverter},ConverterParameter='SHOW LOCATIONS'}">
        <telerik:RadTreeListView Name="LocationTree"
                                 ItemsSource="{Binding Locations}"
                                 SelectedItem="{Binding SelectedLocation,Mode=TwoWay}"
                                 IsReadOnly="True"
                                 AutoGenerateColumns="False"
                                 CanUserFreezeColumns="False"
                                 CanUserReorderColumns="False"
                                 RowIndicatorVisibility="Collapsed"
            <telerik:RadTreeListView.SortDescriptors>
                <telerik:SortDescriptor Member="Name" SortDirection="Ascending"/>
            </telerik:RadTreeListView.SortDescriptors>
            <telerik:RadTreeListView.ChildTableDefinitions>
                <telerik:TreeListViewTableDefinition ItemsSource="{Binding ChildLocations}" />
            </telerik:RadTreeListView.ChildTableDefinitions>
            <telerik:RadTreeListView.Columns>
                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" Width="200">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding IconResourcePath}" Width="16" Height="16" HorizontalAlignment="Left" Margin="0 0 8 0"/>
                                <TextBlock Text="{Binding Name}"/>
                            </StackPanel>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding LocationType.Description}" Header="Type" Width="175" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding CheckDigits}" Header="Check Digits" Width="115" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding DisplayablePalletLimit}" Header="Maximum Pallets" Width="125" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Status}" Header="Status" Width="100" />
            </telerik:RadTreeListView.Columns>

            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu x:Name="GridContextMenu">
                    <telerik:RadContextMenu.Items>
                        <telerik:RadMenuItem Header="Create Child" Command="{Binding CreateChildLocationCommand}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Create.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Header="Edit" Command="{Binding EditLocationCommand}" Style="{StaticResource DefaultContextMenuItem}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Edit.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Header="Delete" Command="{Binding DeleteLocationCommand}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Delete.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem IsSeparator="True" />
                        <telerik:RadMenuItem Header="Enable" Command="{Binding EnableLocationCommand}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Enable.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Header="Disable" Command="{Binding DisableLocationCommand}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Disable.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem IsSeparator="True" />
                        <telerik:RadMenuItem Header="Refresh" Command="{Binding RefreshCommand}">
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="..\Images\16x16\Refresh.png"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                    </telerik:RadContextMenu.Items>
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>

        </telerik:RadTreeListView>

3 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 03 May 2013, 11:57 AM
Hi Richard, 

The AutoExpandGroups property does not work with treeListView since it does not support grouping. You should use the AutoExpandItems property instead. 

As to the not expanding nodes on selected item changed, may I ask you for some additional information? 
Are you using some Load on demand logic in your code?

Hope this helps! 

Regards,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Richard
Top achievements
Rank 1
answered on 03 May 2013, 12:28 PM
Hi Nik,

There's no fancy logic in this one because there are only a few hundred items and no performance issues. I load an ObservableCollection<Location> with my root Each location has a child collection containing further locations, and this continues usually down to three or four levels.

Richard
0
Nick
Telerik team
answered on 06 May 2013, 11:24 AM
Hello Richard,

This seems very strange. May I ask you to open a support ticket and send a sample project that we can debug on our side and see what goes wrong?

Kind regards,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
TreeListView
Asked by
Richard
Top achievements
Rank 1
Answers by
Nick
Telerik team
Richard
Top achievements
Rank 1
Share this question
or