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

How to close the tab ??

6 Answers 462 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
deepak
Top achievements
Rank 1
deepak asked on 30 Dec 2008, 08:41 AM
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..?

6 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 06 Jan 2009, 10:19 AM
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.
0
deepak
Top achievements
Rank 1
answered on 06 Jan 2009, 02:28 PM
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
0
Serrin
Top achievements
Rank 1
answered on 06 Jan 2009, 08:29 PM
Hi Deepak,

Answered in the other thread you started. :)
0
Stuart
Top achievements
Rank 1
answered on 06 Apr 2009, 11:56 PM
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
0
Miroslav
Telerik team
answered on 07 Apr 2009, 07:29 AM
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.
0
Stuart
Top achievements
Rank 1
answered on 07 Apr 2009, 03:56 PM
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

Tags
TabControl
Asked by
deepak
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
deepak
Top achievements
Rank 1
Serrin
Top achievements
Rank 1
Stuart
Top achievements
Rank 1
Share this question
or