How to close the tab ??

7 posts, 1 answers
  1. deepak
    deepak avatar
    3 posts
    Member since:
    Dec 2008

    Posted 30 Dec 2008 Link to this post

    Hi,I am relatively new to SilverLight Applications..
    I  have created a redtab control and some redtab items inside it.I have also constructed a button on one of the redtab item..  on my xaml page, which i want to use for closing that redtab item on the click of that button(which i have designed for this purpose(close tab) only). I am trying to look around for a way to do it in its event handler but can't..Can you please help me..?
  2. Answer
    Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 06 Jan 2009 Link to this post

    Hi deepak,

    Silverlight does not offer a way to register for an event in the DataTemplate of a control, so we have to come up with workarounds.

    On workaround is to add a UserControl in the template that will have a logic to notify when the button is clicked (i.e. the button will be in the UserControl and the UserControl will have access to the button and its properties.

    A more general solution will be to use RoutedEvents which will travel down the visual three and can be handled at a root object. The Telerik controls use Routed Events but unfortunately the default Button does not. Our implementation of the routed events is compatible with all controls, so you could attach a routed event to the button and fire it. Then you can handle the routed event and remove the item.

    I suppose that this is what you are trying to achieve:



    I have attached a sample solution that you can have a look at. I have also cleaned the solution, you may need to include the Telerik controls.

    Sincerely yours,
    Miroslav
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. deepak
    deepak avatar
    3 posts
    Member since:
    Dec 2008

    Posted 06 Jan 2009 Link to this post

    Hi Miroslav ,
    Thanks for providing me with the answer..,actually I had been working on that..and  at lastfound out my way through that user control way that u told me today.. and it worked..!!! but i am thankful to you.. i hope that next time i dont have to mess up my mind.. and i get my reply a bit fast.. Thanks a lot..

    Now I am stuck into another problem  

    Actually i am not able to load my image when i run my application.. it seems its a pity error.. but still.. code seems to be all right to me

      BitmapImage firstImg = new BitmapImage();

     string path = "../_assets/Themes/0/icons/51-icon.png";

     firstImg.UriSource = new Uri(firstUri, UriKind.Relative);

    Image img1;

      img1 = new Image()
                {
                    Source = firstImg,
                    Width = 50,
                    Height = 50,
                    Cursor = Cursors.Hand,
                    VerticalAlignment = VerticalAlignment.Top,
                    HorizontalAlignment = HorizontalAlignment.Right,
                    Visibility = Visibility.Visible
                };

    Then i have defined a grid and tried to add this image to my grid but its not running and an internal exception "Invalid Operation exception" is coming

    g.children.add(img1);

    Any Suggestions where i went wrong please.......
    Thanks a lot
    Deepak
  5. Serrin
    Serrin avatar
    286 posts
    Member since:
    Sep 2008

    Posted 06 Jan 2009 Link to this post

    Hi Deepak,

    Answered in the other thread you started. :)
  6. Stuart
    Stuart avatar
    12 posts
    Member since:
    Feb 2009

    Posted 06 Apr 2009 Link to this post

    Hi Miroslav,

    I'm trying to integrate the solution provided here into my application, which has a slightly different issue than your demo.

    You remove the tab by removing the bound item from the datacontext collection. My tabs are dynamically added based on user action, not from a bound collection.

    How can I access the containing RadTabItem to pass to Remove(), or otherwise remove the correct item from the collection of tabs?

    Thanks,
    Stuart Updegrave
  7. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 07 Apr 2009 Link to this post

    Hi Stuart,

    Once you have an item, you can easily get its parent items control and remove it from there. In this case it may be a good idea to add a TabItem class handeler for the event (a handler for all TabItems). In the particular case, you register the class handler like so:

    static Page()  
    {  
        EventManager.RegisterClassHandler(typeof(RadTabItem), EventHelper.ClickEvent, new RoutedEventHandler(OnCloseClicked));  

    and change the handler accordingly:

    private static void OnCloseClicked(object sender, RoutedEventArgs e)  
    {  
        //in class hanlders the sender is always the object where the event was handled:  
        var tabItem = sender as RadTabItem;  
          
        var parentItemsControl = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(tabItem);  
          
        parentItemsControl.Items.Remove(tabItem);  



    Greetings,
    Miroslav
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  8. Stuart
    Stuart avatar
    12 posts
    Member since:
    Feb 2009

    Posted 07 Apr 2009 Link to this post

    Hi Miroslav,

    Thanks for the reply. It turns out I'd found a solution in the meantime, using VisualTreeHelper.GetParent to access the tab item. However, your solution is a lot cleaner.

    Much appreciated.

    Cheers,
    Stuart Updegrave

Back to Top
DevCraft banner