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

custom button

5 Answers 32 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rodolfo
Top achievements
Rank 1
Rodolfo asked on 12 Oct 2010, 02:35 PM
Hi,
I need to add a button to my treeviewitems in code behind (c#).
I don't find any "How to ", can you please help me?

Regards.
/Rodolfo

5 Answers, 1 is accepted

Sort by
0
Alex Fidanov
Telerik team
answered on 14 Oct 2010, 12:19 PM
Hello Rodolfo,

We would recommend adding the button(s) in the Content Template of the RadTreeViewItem. You could bind the Visibility to a property of the bound business object. As the RadTreeView supports virtualization, the containers are virtualized and the button(s) might be lost. Can you please give us more information on the purpose of the button inside the RadTreeViewItem and is it strictly necessary to add it from procedural code?

Regards,
Alex Fidanov
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
Rodolfo
Top achievements
Rank 1
answered on 15 Oct 2010, 10:59 AM
Hi Alex,

The purpose of the button is to be able to remove the item when you click it.
I be more than happy if you have any other suggestion in how to do this.

Regards
/Rodolfo
0
Alex Fidanov
Telerik team
answered on 15 Oct 2010, 01:37 PM
Hello Rodolfo,

Here is a small code snippet of what I suggested :

<...Resources>
            <HierarchicalDataTemplate x:Key="SampleTemplate" ItemsSource="{Binding Sub}" >
                <DockPanel>
                    <ContentPresenter Content="{Binding Name}" DockPanel.Dock="Left" VerticalAlignment="Center"/>
                    <Button x:Name="DeleteButton" Content="X" DockPanel.Dock="Right" Visibility="{Binding AllowDelete}" VerticalAlignment="Center" Margin="5,0,0,0"/>
                </DockPanel>
            </HierarchicalDataTemplate>
        </....Resources>
        <telerik:RadTreeView ItemTemplate="{StaticResource SampleTemplate}" ItemsSource="{Binding}"/>
This approach targets the data template of the RadTreeViewItem. Another approach would be to place the button in the ControlTemplate of the RadTreeViewItem and again bind the visibility to some property of the RadTreeViewItem or for example the Tag property of the RadTreeView.

Kind regards,
Alex Fidanov
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
Rodolfo
Top achievements
Rank 1
answered on 19 Oct 2010, 12:59 PM
Hi Alex,
Thanks for the code I got from you.
do you have any sample where you do this in code-behind?

Regards
/Rodolfo 
0
Alex Fidanov
Telerik team
answered on 20 Oct 2010, 11:14 AM
Hello Rodolfo,

Theoretically you could do this by traversing the visual tree of the RadTreeView or RadTreeViewItem using the VisualTreeHelper class and finding the right place to insert a button, however it is really not a good practice and I would recommend against it. A less error prone method would be to bind the properties of the button ( like Visibility, Content, Command, etc. and have the button always in the visual tree using this data template) to properties of your underlying business object. This way you will both take advantage of the WPF Binding engine and keep your code much more reliable.

All the best,
Alex Fidanov
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
Rodolfo
Top achievements
Rank 1
Answers by
Alex Fidanov
Telerik team
Rodolfo
Top achievements
Rank 1
Share this question
or