treeviewitem deselection

4 posts, 0 answers
  1. scott
    scott avatar
    2 posts
    Member since:
    Dec 2010

    Posted 11 Feb 2011 Link to this post

    I am populating a treeview with an observeablecollection.  my collection object has a flag for isSelected and the bindng is set up and working.  When I select an item, the appropriate object's isSelected flag shows true.  My objective is to programmatically flip the isSelected to false based upon some criteria.  I am doing it in the code behind as shown below.  I set the isSelected to false, but the control does not show the change, the binding is setup for two way and the flag calls OnPropertyChanged in the set.  I'm clearly missing something.  thoughts?


            private void treeviewProvantage_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadTreeView treeviewProvantage = sender as RadTreeView;
                ProvatageItem pvSelected = treeviewProvantage.SelectedItem as ProvatageItem;
                ItemType selectedItemType = pvSelected.MyType;
                
                foreach (ProvatageItem item in treeviewProvantage.SelectedItems)
                {
                    if (item.MyType != selectedItemType)
                    {
                        item.isSelected = false;
                    }
                }
                MessageBox.Show(treeviewProvantage.SelectedItems.Count.ToString());
                
            }
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 15 Feb 2011 Link to this post

    Hello scott,

    it is hard for us to get quickly into your scenario, since we need the structure of your ViewModel and the settings of the tree. What is the selection mode of the tree? Could you try to use the PreviewChecked event and to make your uncheck logic with setting e.handled = true based on some condition. Basically, every time you set RadTreeViewItem.IsSelected  = true / false directly or via notifying ( with container bindings )  the Selected or Unselected event is fired.  You could send a runnable sample showing the issue and this way we will advice you much better and faster.

    Kind regards,
    Petar Mladenov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. scott
    scott avatar
    2 posts
    Member since:
    Dec 2010

    Posted 15 Feb 2011 Link to this post

    Thanks for taking a look at this....below is the view, codebehind and viewmodel

    <UserControl x:Class="ProquestSLApp.Views.NavigationView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
                 xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
                 mc:Ignorable="d"
        xmlns:telerikdata="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"       
                 xmlns:local="clr-namespace:ProquestSLApp.Views"
                 d:DesignHeight="300" d:DesignWidth="400" Width="300">




        <UserControl.Resources>


            <telerikdata:ContainerBindingCollection x:Name="RadTreeViewBindings">
                <telerikdata:ContainerBinding PropertyName="IsSelected" Binding="{Binding isSelected, Mode=TwoWay}" />
                <!--<telerikdata:ContainerBinding PropertyName="IsExpanded" Binding="{Binding IsExpanded, Mode=TwoWay}" />-->
            </telerikdata:ContainerBindingCollection>


            <telerikdata:HierarchicalDataTemplate x:Key="SubItemsTemplate" ItemsSource="{Binding SubItems}" 
                                                  telerikdata:ContainerBinding.ContainerBindings="{StaticResource RadTreeViewBindings}">
                
                <TextBlock Text="{Binding Text}" />
                
            </telerikdata:HierarchicalDataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">


            <telerik:RadOutlookBar x:Name="outlookBar" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsMinimizable="False">
                
                <telerik:RadOutlookBarItem x:Name="itemProvantage" Header="Provantage" FontWeight="Bold">
                    <telerik:RadTreeView Name="treeviewProvantage" IsLineEnabled="True" ItemsSource="{Binding ProvantageTree}"
                                         ItemTemplate="{StaticResource SubItemsTemplate}" SelectionMode="Extended" SelectionChanged="RadTreeView_SelectionChanged"  ItemDoubleClick="treeviewProvantage_ItemDoubleClick" PreviewSelected="treeviewProvantage_PreviewSelected" Selected="treeviewProvantage_Selected">
                    </telerik:RadTreeView>
                </telerik:RadOutlookBarItem>
                
                <telerik:RadOutlookBarItem x:Name="itemLeads" Header="Leads" FontWeight="Bold">
                </telerik:RadOutlookBarItem>

                <telerik:RadOutlookBarItem x:Name="itemShowingFeedback" Header="Showing Feedback" FontWeight="Bold" >
                </telerik:RadOutlookBarItem>


                <telerik:RadOutlookBarItem x:Name="itemVideoEmail" Header="Video Email" FontWeight="Bold" Tag="Contacts">                
                </telerik:RadOutlookBarItem>


                <telerik:RadOutlookBarItem x:Name="itemProquestStore" Header="Proquest Store" FontWeight="Bold">
                    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Margin="15 5">
                        <RadioButton Content="To-Do List" IsChecked="True" Margin="0 5 0 5" />
                        <RadioButton Content="Simple List" Margin="0 5 0 5" />
                        <RadioButton Content="Detailed List" Margin="0 5 0 5" />
                        <RadioButton Content="Active Tasks" Margin="0 5 0 5" />
                        <RadioButton Content="Next Seven Days" Margin="0 5 0 5" />
                        <RadioButton Content="Overdue Tasks" Margin="0 5 0 5" />
                    </StackPanel>
                </telerik:RadOutlookBarItem>
                
            </telerik:RadOutlookBar>
        </Grid>
    </UserControl>


    namespace ProquestSLApp.Views
    {
        [View(typeof(NavigationViewModel))]
        public partial class NavigationView : UserControl
        {
            public NavigationView()
            {
                InitializeComponent();
           
                //this.treeviewProvantage.Items.Add("Test");
            }


            private void RadTreeView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
            {
                //RadTreeView treeView = sender as RadTreeView;
                //ProvatageItem pv = treeView.SelectedItem as ProvatageItem;
               
                //foreach (ProvatageItem item in treeView.SelectedItems)
                //{
                //    // AppContext.Current.ViewService.GetViewManager("Main").ShowView(typeof(HomeViewModel));
                //    MessageBox.Show(item.Text);
                //}


            }


            private void treeviewProvantage_ItemDoubleClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
            }


            private void treeviewProvantage_PreviewSelected(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {   
            }


            private void treeviewProvantage_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadTreeView treeviewProvantage = sender as RadTreeView;
                ProvatageItem pvSelected = treeviewProvantage.SelectedItem as ProvatageItem;
                ItemType selectedItemType = pvSelected.MyType;


                //List<ProvatageItem> listToClear = treeviewProvantage.SelectedItems.Where(p => (p as ProvatageItem).MyType != selectedItemType).Cast<ProvatageItem>().ToList();


                //listToClear.ForEach(i => i.isSelected = false);




                foreach (ProvatageItem item in treeviewProvantage.SelectedItems)
                {
                    if (item.MyType != selectedItemType)
                    {
                        item.isSelected = false;


                    }
                }
                AppContext.Current.ViewService.GetViewManager("Main").ShowView(typeof(ExtensionViewModel));
                MessageBox.Show(treeviewProvantage.SelectedItems.Count.ToString());
                
            }
        }
    }


    namespace ProquestSLApp.Views
    {
        [DockWindow(PaneGroup = "LeftPane", CanUserClose = false)]
        public class NavigationViewModel : BaseViewModel
        {


           #region ProvantageTree Property
           private ObservableCollection<ProvatageItem> _ProvantageTree;
           public ObservableCollection<ProvatageItem> ProvantageTree
           {
               get { return this._ProvantageTree; }
               set
               {
                   if (this._ProvantageTree != value)
                   {
                       this._ProvantageTree = value;
                       this.OnPropertyChanged("ProvantageTree");
                       this.OnProvantageTreeChanged();
                   }
               }
           }
           protected virtual void OnProvantageTreeChanged() { }
           #endregion
            public override object Initialize(object Parameters)
            {
                Title = "Navigation View";
                ProvantageTree = new ObservableCollection<ProvatageItem>();
                populateProvatangeTree();


                return null;
            }


            private void populateProvatangeTree()
            {
                ProvantageTree.Add(new ProvatageItem()
                {
                    Text = "800-959-3959",
                    MyType = ItemType.DNIS,
                    SubItems =
                {
                    new ProvatageItem()
                    {
                        Text = "Greetings",
                        MyType = ItemType.Greeting,
                        SubItems =
                        {
                            new ProvatageItem()
                            {
                                Text = "Business Hours",
                                MyType = ItemType.Greeting,
                            },
                            new ProvatageItem()
                            {
                                Text = "After Hours",
                                MyType = ItemType.Greeting,
                            },
                            new ProvatageItem()
                            {
                                Text = "WeekEnd",
                                MyType = ItemType.Greeting,
                            },
                            new ProvatageItem()
                            {
                                Text = "Special",
                                MyType = ItemType.Greeting,
                            }
                        }
                    },
                    new ProvatageItem()
                    {
                        Text = "Extensions",
                        MyType = ItemType.Extension,
                        SubItems =
                        {
                            new ProvatageItem()
                            {
                                Text = "101",
                                MyType = ItemType.Extension,
                            },
                            new ProvatageItem()
                            {
                                Text = "102",
                                MyType = ItemType.Extension,
                            },
                            new ProvatageItem()
                            {
                                Text = "105",
                                MyType = ItemType.Extension,
                            },
                            new ProvatageItem()
                            {
                                Text = "109",
                                MyType = ItemType.Extension,
                            }
                        }
                    }              
                }
                });
                ProvantageTree.Add(new ProvatageItem()
                {
                    Text = "Notifiers",
                    MyType = ItemType.Notifiers,
                    SubItems = 
                    {
                        new ProvatageItem()
                        {
                            Text = "618-555-1212",
                            MyType = ItemType.Notifiers
                        },
                        new ProvatageItem()
                        {
                            Text = "618-222-1212",
                            MyType = ItemType.Notifiers
                        },
                        new ProvatageItem()
                        {
                            Text = "scott.wood@proquest-tech.com",
                            MyType = ItemType.Notifiers
                        }
                    }
                });
            }
        }
    }
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 17 Feb 2011 Link to this post

    Hi scott,

    As I managed to understand, your wish is to disable selecting both Extensions and Greetings.
    First of all, a possible problem is that when you are "foreach-ing" SelectedItems collections and set IsSelected in the body of the foreach statement, the collection gets changed, indices are rearranged and etc. which may lead to various issues. Secondly, its bad practice to set Selected properties in selected event since this again fires selected event and you have to write additonal code to prevent cycles, recursions ... the PreviewSelected and PreviewSelectionChanged events are a bit better idea but still not a good practice in this scenario. Additionally , you should be aware that in extended selection mode, the following is always true; tree.SelectedItem == tree.SelectedItems[0]. So you cannot rely that the SelectedItem in the Selected event handler is the last selected : SelectedItems[n - 1].
    However,  I modified your solution to use the ItemClick event of the RadTreeView. I check whether the currently clicked item`s type is different from the SelectedItems`type ( I guess you wish this collection to include elements of single type). If so, clear the SelectedItems and selected the clicked item.
    Please let me know if this approach satisfies you.

    privatevoidRadTreeView_SelectionChanged(objectsender, Telerik.Windows.Controls.SelectionChangedEventArgs e)

    {

    }

    privatevoidtreeviewProvantage_ItemDoubleClick(objectsender, Telerik.Windows.RadRoutedEventArgs e)

    {

    }

    privatevoidtreeviewProvantage_PreviewSelected(objectsender, Telerik.Windows.RadRoutedEventArgs e)

    {

    }

    privatevoidtreeviewProvantage_Selected(objectsender, Telerik.Windows.RadRoutedEventArgs e)

    {

          

    }

    privatevoidtreeviewProvantage_ItemClick(objectsender, Telerik.Windows.RadRoutedEventArgs e)

    {

        var item = (e.OriginalSource asRadTreeViewItem).Item;

        ItemType selectedItemsType = (this.treeviewProvantage.SelectedItem asProvatageItem).MyType;

        if(selectedItemsType != (item asProvatageItem).MyType)

        {

            this.treeviewProvantage.SelectedItems.Clear();

        }

        (item asProvatageItem).isSelected = true;

    }



    Best wishes,
    Petar Mladenov
    the Telerik team
Back to Top