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

Expose RadTreeView.SelectedContainer property

3 Answers 89 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
manjerekar
Top achievements
Rank 1
manjerekar asked on 30 Aug 2010, 02:58 PM
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

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 01 Sep 2010, 12:19 PM
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
0
manjerekar
Top achievements
Rank 1
answered on 01 Sep 2010, 03:49 PM
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
0
Hristo
Telerik team
answered on 06 Sep 2010, 01:53 PM
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
Tags
TreeView
Asked by
manjerekar
Top achievements
Rank 1
Answers by
Hristo
Telerik team
manjerekar
Top achievements
Rank 1
Share this question
or