Default RadTabItem class for RadTabbedWindow.AddingNewTab

1 Answer 34 Views
TabbedWindow
Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
Timothy J asked on 22 Nov 2022, 08:25 PM

I would like to create a custom "control" that is a subclass of "RadTabItem" and use it as the default tab class.

I know I can tie into the AddingNewTab event and effect the Item property of the event args, but that would be a UI Element in the ViewModel (VM) of a MVVM and I would like to keep the UI element strictly in the UI code (not in the VM).

Is there some way to specify (perhaps via .Resources) the class for a new tab created by clicking the "+" in the RadTabbedWindow.

e.g.  something like the a "DefaultTab" element.

          <DefaultTab Type="{x:Type MyTabClass}"/>

of the below


<telerik:RadTabbedWindow x:Class="PickCart.Client.Zza.Desktop.Tabs.TabbedView" 
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                         xmlns:client="clr-namespace:PickCart.Client"
                         xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
                         Width="1024"
                         Height="800">
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="AddingNewTab">
            <b:CallMethodAction TargetObject="{Binding}" MethodName="OnAddingNewTab"/>
        </b:EventTrigger>
    </b:Interaction.Triggers>
    <telerik:RadTabbedWindow.DataContext>
        <client:TabbedViewModel/>
    </telerik:RadTabbedWindow.DataContext>
    <telerik:RadTabbedWindow.Resources>
          <DefaultTab Type="{x:Type MyTabClass}"/>
    </telerik:RadTabbedWindow.Resources>
    <telerik:RadTabItem Header="Main" CloseButtonVisibility="Hidden">
        <TextBlock Text="This is the main tab" />
    </telerik:RadTabItem>
</telerik:RadTabbedWindow>

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 24 Nov 2022, 12:23 PM

Hello Timothy,

Thank you for the provided code snippet.

Technically speaking, you can replace the TabbedWindowTabControl used in the control template of the RadTabbedWindow with a custom implementation that overrides the GetContainer method:

	public class CustomTabbedWindowTabControl : TabbedWindowTabControl
	{
            protected override IRadTabItem GetContainer()
            {
			return new MyTabItem();
            }
        }

Please note that this custom item type needs to implement the IRadTabItem interface (possibly by inheriting from RadTabItem).

For your convenience, I've prepared a small sample project demonstrating this in action.

Can you, however, specify why you need to create a custom tab item class? There may be a more straightforward solution I can offer for your particular case, such as defining an ItemTemplate, for example.

Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 24 Nov 2022, 12:31 PM

>>

Can you, however, specify why you need to create a custom tab item class? There may be a more straightforward solution I can offer for your particular case, such as defining an ItemTemplate, for example.<<

 


At runtime, we will only be adding one "type" (one business function) type of tab.  The custom tab will be a self contained MVVM.  Just made sense that if the user clicks the "+" button that no logic needs to be applied except putting the correct model into place as the new tab and let the tab VM (and View) take it from there.

Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 24 Nov 2022, 02:15 PM

Further information, our ViewModel is located in an assembly that does not have a reference to System.Windows, so even reading the 'AddingNewTabEventArgs' is problematic.  I went with the below.

 


        public TabbedView()
        {
            InitializeComponent();
            this.AddingNewTab += OnAddingNewTab;
        }

        private void OnAddingNewTab(object? sender, AddingNewTabEventArgs e)
        {
            var batchTab = AppHost.Provider.GetRequiredService<IBatchTab>();
            batchTab.DataContext = AppHost.Provider.GetRequiredService<BatchTabViewModel>();
            e.Item = batchTab;
        }

Dilyan Traykov
Telerik team
commented on 25 Nov 2022, 12:51 PM

Thank you for the clarification and the provided code snippet.

I could not infer, however, whether you found the provided suggestion helpful and if you managed to achieve the desired result. Can you please specify whether this is the case?

If not, please modify the sample project I provided to better demonstrate your setup and requirement and I will gladly assist you further.

Tags
TabbedWindow
Asked by
Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or