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

SelectionChanged of RadTreeListView fired multiple times

1 Answer 122 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Ahmed
Top achievements
Rank 1
Ahmed asked on 20 Sep 2015, 10:57 AM

Hey, i have a RadTreeListView that i used to display my data, when i change the value of RowDetailsTemplate property in runtime, the event that called SelectionChanged fired more than 15 times, here is my code:

 

XAML Code:

<telerik:RadTreeListView ItemsSource="{Binding ItemSearchingResults}"
                                                 RowDetailsVisibilityMode="VisibleWhenSelected"
                                                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                                                 RowDetailsTemplate="{Binding RowDetailsTemplate}"
                                                 ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                                 SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                                                 CanUserFreezeColumns="False" SelectionMode="Single"
                                                 VerticalAlignment="Stretch" SelectionUnit="FullRow"
                                                 CanUserDeleteRows="False" GridLinesVisibility="Both"
                                                 CanUserInsertRows="False" AutoGenerateColumns="False"
                                                 AutoLoadHierarchy="True" HierarchyColumnIndex="1" HierarchyIndent="25"
                                                 RowIndicatorVisibility="Collapsed" ShowColumnFooters="False" HorizontalAlignment="Stretch">

                            <telerik:RadTreeListView.Columns>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemNumber}" IsReadOnly="True"
                                                            Header="{x:Static tterpResources:UcItemSearching.Number}" Width="*" />
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemArabicName}" IsReadOnly="True"
                                                            Header="{x:Static tterpResources:UcItemSearching.ArabicName}" Width="*" />
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemEnglishName}" IsReadOnly="True"
                                                            Header="{x:Static tterpResources:UcItemSearching.EnglishName}" Width="*" />
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemGroupArabicName}" IsReadOnly="True"
                                                            Header="{x:Static tterpResources:UcItemSearching.Group}" Width="*" />
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemTypeArabicName}" IsReadOnly="True"
                                                            Header="{x:Static tterpResources:UcItemSearching.Type}" Width="*" />
                            </telerik:RadTreeListView.Columns>

                            <interactivity:Interaction.Triggers>
                                <interactivity:EventTrigger EventName="SelectionChanged">
                                    <interactions:CallMethodAction MethodName="OnSelectItemChanged" TargetObject="{Binding}" />
                                </interactivity:EventTrigger>
                            </interactivity:Interaction.Triggers>

                            <telerik:RadTreeListView.ChildTableDefinitions>
                                <telerik:TreeListViewTableDefinition ItemsSource="{Binding ChildItems, 
                                                                     RelativeSource={RelativeSource AncestorType=models:ItemSearchingModel}}" />
                            </telerik:RadTreeListView.ChildTableDefinitions>

                        </telerik:RadTreeListView>

 

C# Code:

public void OnSelectItemChanged()
        {
            if (SelectedItem == null || !SelectedItem.IsDataChanged) return;

            //Get child items of selected item.
            if (!SelectedItem.IsChildLoadedOnce)
            {
                SelectedItem.IsChildLoadedOnce = true;
                SelectedItem.ChildItems = GetChildItemsOfParentItem(SelectedItem.ItemTreeId);
            }

            //Get alternative items of selected item.
            if (!SelectedItem.IsAlternativeLoadedOnce)
            {
                SelectedItem.IsAlternativeLoadedOnce = true;
                SelectedItem.AlternativeItems = GetAlternativeItemsOfSelectedItem(SelectedItem.ItemTreeId);
            }

            RowDetailsTemplate = SelectedItem.AlternativeItems.Count > 0 ? Application.Current.FindResource("AlternativeItemsDataTemplate") as DataTemplate : null;
        }

        private ObservableCollection<ItemSearchingModel> GetChildItemsOfParentItem(long parentItemId)
        {
            var childItems = _erpEntities.ItemTrees.Where(itemTree => itemTree.ParentId == parentItemId);
            if (!childItems.Any()) return new ObservableCollection<ItemSearchingModel>();

            var childItemSearchingModelList = new ObservableCollection<ItemSearchingModel>();
            foreach (var childItemTree in childItems)
            {
                childItemSearchingModelList.Add(new ItemSearchingModel
                {
                    ItemTreeId = childItemTree.Id,
                    ItemNumber = childItemTree.Number,
                    ItemArabicName = childItemTree.NameArabic,
                    ItemEnglishName = childItemTree.NameEnglish,
                    ChildItems = new ObservableCollection<ItemSearchingModel>(),
                    AlternativeItems = new ObservableCollection<ItemSearchingModel>(),
                    ItemTypeArabicName = childItemTree.ItemsType != null ? childItemTree.ItemsType.NameArabic : string.Empty,
                    ItemTypeEnglishName = childItemTree.ItemsType != null ? childItemTree.ItemsType.NameEnglish : string.Empty,
                    ItemGroupArabicName = childItemTree.ItemsGroup != null ? childItemTree.ItemsGroup.NameArabic : string.Empty,
                    ItemGroupEnglishName = childItemTree.ItemsGroup != null ? childItemTree.ItemsGroup.NameEnglish : string.Empty
                });
            }
            return childItemSearchingModelList;
        }

        private ObservableCollection<ItemSearchingModel> GetAlternativeItemsOfSelectedItem(long selectedItemId)
        {
            //Get the child items.
            var alternativeItems = _erpEntities.AlternativeItems.Where(itemTree => itemTree.BasicItemTreeId == selectedItemId);
            if (!alternativeItems.Any()) return new ObservableCollection<ItemSearchingModel>();

            var alternativeItemsSearchingModelList = new ObservableCollection<ItemSearchingModel>();
            foreach (var childItemTree in alternativeItems)
            {
                alternativeItemsSearchingModelList.Add(new ItemSearchingModel
                {
                    ItemTreeId = childItemTree.Id,
                    IsAlternativeLoadedOnce = true,
                    ItemNumber = childItemTree.ItemTree.Number,
                    ItemArabicName = childItemTree.ItemTree.NameArabic,
                    ItemEnglishName = childItemTree.ItemTree.NameEnglish,
                    ChildItems = new ObservableCollection<ItemSearchingModel>(),
                    AlternativeItems = new ObservableCollection<ItemSearchingModel>(),
                    ItemTypeArabicName = childItemTree.ItemTree.ItemsType != null ? childItemTree.ItemTree.ItemsType.NameArabic : string.Empty,
                    ItemTypeEnglishName = childItemTree.ItemTree.ItemsType != null ? childItemTree.ItemTree.ItemsType.NameEnglish : string.Empty,
                    ItemGroupArabicName = childItemTree.ItemTree.ItemsGroup != null ? childItemTree.ItemTree.ItemsGroup.NameArabic : string.Empty,
                    ItemGroupEnglishName = childItemTree.ItemTree.ItemsGroup != null ? childItemTree.ItemTree.ItemsGroup.NameEnglish : string.Empty
                });
            }
            return alternativeItemsSearchingModelList;
        }

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 23 Sep 2015, 03:00 PM
Hello Ahmed,

From the provided code snippet I am not able to clarify the exact cause for such behavior. Would it be possible for you to provide a runnable demo that demonstrates the issue, or the full code so that I can build a sample project on my side?

Regards,
Stefan X1
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
TreeListView
Asked by
Ahmed
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or