custom button

6 posts, 0 answers
  1. Rodolfo
    Rodolfo avatar
    5 posts
    Member since:
    Sep 2010

    Posted 12 Oct 2010 Link to this post

    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
  2. Alex Fidanov
    Admin
    Alex Fidanov avatar
    637 posts

    Posted 14 Oct 2010 Link to this post

    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
  3. Rodolfo
    Rodolfo avatar
    5 posts
    Member since:
    Sep 2010

    Posted 15 Oct 2010 Link to this post

    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
  4. Alex Fidanov
    Admin
    Alex Fidanov avatar
    637 posts

    Posted 15 Oct 2010 Link to this post

    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
  5. Rodolfo
    Rodolfo avatar
    5 posts
    Member since:
    Sep 2010

    Posted 19 Oct 2010 Link to this post

    Hi Alex,
    Thanks for the code I got from you.
    do you have any sample where you do this in code-behind?

    Regards
    /Rodolfo 
  6. Alex Fidanov
    Admin
    Alex Fidanov avatar
    637 posts

    Posted 20 Oct 2010 Link to this post

    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
Back to Top