Hi,
When you define a visual element within a
DataTemplate, this element is generated and loaded when the
DataTemplate gets applied. Additionally as the element is not directly part of the visual tree (as it gets applied as part of a
DataTemplate), it is not a straight-forward task to access it. Basically what you can do is implement a code-behind logic that traverses the visual tree to find the
RadGridView after the
ContentTemplate in which it is defined is applied.
For that purpose you first need to consider the fact that
RadTabControl loads the
Content of its tabs on demand. This means that if the first item is selected its
Content will be loaded. Then when you select another item the
Content of the first one will be removed from the visual tree and the
Content of the currently selected item will be generated.
This is why in order to get the
RadGridView you can either:
- handle the RadGridView Loaded event. If you define a GridView in every tab then this would mean that the Loaded event will be invoked when the tab selection changes (when a new RadGridView instance is created). Then you can set the sender as a CommandParameter of a Button. You can see this implementation in the attached project. Also here is an example in code:
- handle the RadTabControl SelectionChanged event to get the RadGridView with the ChildrenOfType<T>() method:
Both options traverse the visual tree which sometimes can be a heavy task. This is why if you can share more information about your requirements and why you need to pass the
RadGridView as a
CommandParameter we might be able to suggest a different approach.
For instance, if you need to use a
GridViewCommand, you can place the Button on a
RadToolBar. The
ToolBar is a focus scope and therefore it doesn't steal the focus away from the other elements on the screen. This allows its buttons to detect other focused elements and use them as targets of their commands.
Regards,
Martin
Telerik
DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.