Toolbar as part of a bigger control

2 posts, 0 answers
  1. Ben Boger
    Ben Boger avatar
    19 posts
    Member since:
    May 2010

    Posted 09 Jun 2010 Link to this post


    I have a basic control that will be used in different areas. It has a couple of toolbars on it. I need to be able to add buttons to the toolbars in the control later on and I can do this right now no problem through code. 

    What I would like to be able to do is add the buttons in the XAML file like so
                  
    <Controls:SomeControl  Name="smartList" ToolbarTopVisibility="Collapsed">  
                <Controls:SomeControl.Toolbar1> 
                    <Button Name="_addButton" Margin="0,5,0,0" Click="AddButtonClick" 
                            Content="{Binding Path=ApplicationStrings.Add, Source={StaticResource ResourceWrapper}}"/>  
                    <Button Name="_editButton" Margin="0,5,0,0" Click="EditButtonClick" 
                            Content="{Binding Path=ApplicationStrings.Edit, Source={StaticResource ResourceWrapper}}"/>  
                    <Button Name="_deleteButton" Margin="0,5,0,0" Click="DeleteButtonClick" 
                            Content="{Binding Path=ApplicationStrings.Delete, Source={StaticResource ResourceWrapper}}"/>  
        
                </Controls:SomeControl.Toolbar1> 
            </Controls:SomeControl> 

    as shown below, by adding a property ( Toolbar1 ) to my Control and returning the Items in the RadToolBar I am able to have the above code working, events fire and everything in general works just as you would expect.
    public class SomeControl     
    {   
        public SomeControl()     
        {     
            InitializeComponent();     
        }     
        
        public ItemCollection RightButtons     
        {     
            getreturn Toolbar1.Items; }     
        }     
    }    
     


    The problem I am having is I would like to be able to access the buttons for enabling and disabling using the names declared in the XAML file. but when I try to do this the controls are null. the Items in the toolbar they are declared under are not null.

    Is there a way to make sure the controls I declared are not null so that I can access the buttons or any other control from the name I have given it?
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 14 Jun 2010 Link to this post

    Hi Ben,

    For this particular scenario you will have to loop through ToolBar's items and find the desired buttons. Something like this:

    private void ToggleIsEnabled_Click(object sender, RoutedEventArgs e)
    {
        // initially, the _add button is null, therefore we need to find it.
        if (_add == null)
        {
            var result = (from n in slControl1.ToolBar1.Items
                    where (n as Control).Name == "_add"
                    select n).FirstOrDefault();
                     
            _add = result as Button;
        }
                 
        // do whatever you want with the _add button
        _add.IsEnabled = !_add.IsEnabled;
    }

    I am attaching my sample project for further reference. Have a look at it and let me know if it helps. I'd be glad to further assist you.

    Greetings,
    Kiril Stanoev
    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. DevCraft banner
Back to Top