Expose RadTreeView.SelectedContainer property

4 posts, 0 answers
  1. manjerekar
    manjerekar avatar
    10 posts
    Member since:
    Jul 2010

    Posted 30 Aug 2010 Link to this post

    Hi Team,

    How can i expose RadTreeView.SelectedContainer property,  which will return my own custom type instead of RadTreeViewItem?

    code:

     

     

    public RadTreeViewItem SelectedContainer

     

    {

     

     

    get { return (RadTreeViewItem)TreeView.GetValue(RadTreeView.SelectedContainerProperty); }

     

     

     

    set { TreeView.SetValue(RadTreeView.SelectedContainerProperty, value); }

     

    }


    instead of the above code i want to write something like this:

     

     

    public TngTreeViewItem SelectedContainer

     

    {

     

     

    get { return (TngTreeViewItem )TreeView.GetValue(RadTreeView.SelectedContainerProperty); }

     

     

     

    set { TreeView.SetValue(RadTreeView.SelectedContainerProperty, value); }

     

    }

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------
    TngTreeViewItem.cs

    [

     

    ContentProperty("Items")]

     

     

     

    public class TngTreeViewItem : FrameworkContentElement

     

    {

     

     

    protected RadTreeViewItem _treeViewItem = new RadTreeViewItem();

     

    #region

     

     

    Constructors

     

     

     

    static TngTreeViewItem()

     

    {

    DefaultStyleKeyProperty.OverrideMetadata(

     

    typeof(TngTreeViewItem), new FrameworkPropertyMetadata(typeof(TngTreeViewItem)));

     

    }

    #endregion

    #region

     

     

    Internal Control

     

     

     

    public RadTreeViewItem InternalControl

     

    {

     

     

    get

     

    {

     

     

    return _treeViewItem;

     

    }

    }

    #endregion

     

    #region

     

     

    Property: Header

     

     

     

    public virtual Object Header

     

    {

     

     

    get { return GetValue(HeaderProperty); }

     

     

     

    set { SetValue(HeaderProperty, value); }

     

    }

     

     

    public static readonly DependencyProperty HeaderProperty =

     

     

     

    DependencyProperty.Register(

     

     

     

    "Header", typeof(Object), typeof(TngTreeViewItem),

     

     

     

    new FrameworkPropertyMetadata

     

    {

    PropertyChangedCallback = (obj, e) =>

    {

    (obj

     

    as TngTreeViewItem).UpdateHeader(e.NewValue);

     

    }

    });

     

     

    private void UpdateHeader(Object sel)

     

    {

    _treeViewItem.Header = sel;

    }

    #endregion

    }
    }


    Thanks,
    -Manjurekar
  2. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 01 Sep 2010 Link to this post

    Hi manjerekar,

    I've attached you a sample project demonstrating how to implement your own SelectedContainer property. I hope this will help you solve the issue. If not, can you elaborate a little bit more on what you are trying to achieve.

    Kind regards,
    Hristo Milyakov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. manjerekar
    manjerekar avatar
    10 posts
    Member since:
    Jul 2010

    Posted 01 Sep 2010 Link to this post

    Hi Hristo Milyakov

    Thank you for the example. 

    one question here. can we avoid using the RadRoutedEventArgs in the client application. instead of that i want to use my custom dependency property which wraps even the events. 

    I dont want to use any Telerik's reference's in the client application.

    private void SampleTreeView_Selected(object sender, RadRoutedEventArgs e)
    {
    MyTreeViewItem item = SampleTreeView.SelectedContainer;
    MessageBox.Show(item.Header + " - " + item.MyCustomProperty);
    }


    thank you
    Manjerekar
  5. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 06 Sep 2010 Link to this post

    Hello manjerekar,


    It is possible to do that. You need to add new definition of SelectedEvent in the MyTreeViewItem class with the type of MyRoutedEventArgs.

    new public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("Selected", RoutingStrategy.Bubble, typeof(EventHandler<MyRoutedEventArgs>), typeof(MyTreeViewItem));


    Next step is to implement new Selected setter and getter with the type of EventHandler<MyRoutedEvnetArgs>.

    public new event EventHandler<MyRoutedEventArgs> Selected
    {
        add
        {
            this.AddHandler(MyTreeViewItem.SelectedEvent, value);
        }
        remove
        {
            this.RemoveHandler(MyTreeViewItem.SelectedEvent, value);
        }
    }


    Now you will have your own SelectedEvent with the customized event args type. Next you need to fire the event in appropriate conditions. This could be achieved if you register a handler to the original SelectedEvent (with event args of type RadRoutedeventArgs) of the base RadTreeView class. From the handler you can fire your custom. Following code snippet demonstrates what I mean:

    public MyTreeView() : base()
    {
        (this as RadTreeView).Selected += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(MyTreeView_Selected);
    }
      
    void MyTreeView_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        //If e.Handeled=true is not commented then event handlers registered to both type of events will be executed.
        e.Handled = true;
      
        MyRoutedEventArgs me = new MyRoutedEventArgs();
        //Pass the usefull data from RadRoutedEventArgs to your custom event args object.
        //...
        //...
        //...
        me.RoutedEvent = MyTreeViewItem.SelectedEvent;
        this.RaiseEvent(me);
    }


    Finally you need to register an event handler for SelectedEvent of the MyTreeView object.

    I'm attaching a sample project demonstrating the suggested solution. Please let us know if it works for you.


    Kind regards,
    Hristo Milyakov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top