locked
Custom control (Treeview): show detail if Custom control SelectionChanged RRS feed

Answers

  • Hi,

    You will have to bring out the SelectedItemChanged of the Silverlight control I thinks... Here it goes (sorry if the code is messy, i ran through it).

     - On the XAML, add the SelectedItemChanged Event to the treeview:


     - On the XAML's code behind, add the following to expose the new selected item.
    public SilverlightTreeControl()
            {
                InitializeComponent();
            }
    
    private object selectedItem;
    
    public object SelectedItem { 
                get{
                    return selectedItem;
                }
                set
                {
                    if (value != null)
                    {
                        selectedItem = value;
                    }
                } 
            }
    
    public event SelectedItemChangedEventHandler SelectedItemChanged;
    public delegate void SelectedItemChangedEventHandler(object sender, RoutedPropertyChangedEventArgs<object> e);
    private void tvSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                if (e != null)
                {
                    SelectedItemChanged(sender, e);
                    if (e.NewValue != null)
                    {
                        SelectedItem = e.NewValue;
                    }
                }
            }



     - On your screen,  try to have something like this to test it out:



     - On your screen's created method, grab the control, subscribe to the event, and set the new selected item to your Category selectedItem:

    SilverlightClassLibrary1.SilverlightTreeControl tree = null;
    
    partial void EditableCategoriesGrid_Created()
            {
                this.FindControl("ScreenContent").ControlAvailable += ScreenContentAvailable;
    
            }
    
    private void ScreenContentAvailable(object sender, ControlAvailableEventArgs e)
            {
                if (tree == null)
                {
                    tree = (SilverlightClassLibrary1.SilverlightTreeControl)e.Control;
                    tree.SelectedItemChanged += tvSelectedChanged;
                }
            }
    
    private void tvSelectedChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
            {
                if (e.NewValue != null)
                {
                     Details.Dispatcher.BeginInvoke(() =>
                         {
                            Category cat = (Category)e.NewValue;
                            Categories.SelectedItem = cat;
                     });
                }
            }



    That's about it, I'm sure it can be improved a lot, good luck :)

    • Marked as answer by andrey_vb Thursday, August 13, 2015 6:17 AM
    Wednesday, August 12, 2015 9:44 PM