RadToolbarTray ItemTemplateSelector problems

0 Answers 88 Views
ToolBar
Kim
Top achievements
Rank 1
Kim asked on 30 Apr 2021, 09:16 AM

Hi!

I have my own toolbar class, derived directly from RadToolbar:

<telerik:RadToolBar x:Class="MyNS.Components.MyToolbar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 

             xmlns:viewmodel="clr-namespace:MyNS.ViewModel;assembly=MyNS.ViewModel" 
             d:DataContext="{d:DesignInstance Type=viewmodel:ToolbarViewModel}"

             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             VerticalAlignment="Top"

             DataContext="{Binding}"
             Visibility="{Binding ToolbarVisible, Converter={StaticResource BoolToVisibility}}"
             ItemTemplateSelector="{StaticResource ToolbarElementTemplateSelector}"
             ItemsSource="{Binding Elements}"

             Band="{Binding Band}"
             BandIndex="{Binding BandIndex}"                    
            >
</telerik:RadToolBar>

with

public partial class MyToolbar : RadToolBar
{
public MyToolbar()
{
InitializeComponent();
}
}

When I add this toolbar design time on my child forms it works nicely. The underlying ToolbarViewModel binds all the way, and I get the buttons I want when I run the app. 

But when dynamically instantiating new ToolbarViewModels to be shown on my MainWindow, I get empty buttonless RadToolbar instances, not MyToolbar instances. So I want to control what kind of toolbar gets instantiated. I have this in MainWindow.xaml:

<telerik:RadWindow x:Class="MyNS.MainWindow"
...

<telerik:RadToolBarTray
  Name="Toolbars"
  Grid.Row="1"
  ItemsSource="{Binding Path=Toolbars}"
  IsLocked="False"
  ItemTemplateSelector="{StaticResource ToolbarTemplateSelector}"
/>

</telerik:RadWindow>

The binding itself works. I get RadToolbars, but without any buttons on them. But I get the wrong kind of toolbar, and I thought the ToolbarTemplateSelector would do it, but it never gets called.

So I have:

public class ToolbarTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// currently only one toolbar template

// THIS CODE IS NEVER RUN

return this.MyToolbarTemplate;
}

public DataTemplate MyToolbarTemplate { get; set; }
}

and DataTemplate.xaml

<ResourceDictionary 
...

<!-- compile error if removed, so the mainwindow reference correctly points to this -->
<vm:ToolbarTemplateSelector 
        x:Key="ToolbarTemplateSelector"
        MyToolbarTemplate="{StaticResource dtMyToolbar}"
    />

</ResourceDictionary>

and in PaneTemplate.xaml I have:

<ResourceDictionary 
...

<!-- app crashes on start if this is removed -->
    <DataTemplate x:Key="dtMyToolbar" DataType="{x:Type vm:ToolbarViewModel}">
        <components:MyToolbar />
    </DataTemplate>

</ResourceDictionary>

In my mind, whenever a new ToolbarViewModel is added to the Toolbars collection, the ToolbarTemplateSelector should instantiate a MyToolbar instance for it, and not a RadToolbar instance. 

I have no idea what's missing. 

Oh, I also tested with:

<telerik:RadWindow x:Class="MyNS.MainWindow"
...

<telerik:RadToolBarTray
  Name="Toolbars"
  Grid.Row="1"
  ItemsSource="{Binding Path=Toolbars}"
  IsLocked="False"
>
<DataTemplate>
<ksc:MyToolbar/>
</DataTemplate>
</telerik:RadToolBarTray>
...
</telerik:RadWindow>

But then the app crashes with the error message "Items collection must be empty before using ItemsSource." before the main window shows up. I don't see why that would happen, as the toolbartray is definitely empty at first.

Appreciate any help,

Kim

                                                         
Kim
Top achievements
Rank 1
commented on 30 Apr 2021, 06:05 PM

Doesn't work with a normal RadToolbar either:

<telerik:RadToolBarTray
Name="Toolbars"
Grid.Row="1"
IsLocked="False"
ItemsSource="{Binding Path=Toolbars}"
>

<telerik:RadToolBarTray.ItemTemplate>
<DataTemplate>
<telerik:RadToolBar
Band="{Binding Band}"
BandIndex="{Binding BandIndex}"
ItemsSource="{Binding Elements}"
ItemTemplateSelector="{StaticResource ToolbarElementTemplateSelector}"
>
</telerik:RadToolBar>
</DataTemplate>
</telerik:RadToolBarTray.ItemTemplate>
</telerik:RadToolBarTray>

The toolbars show up, but no buttons...

Cheers,

Kim
Martin Ivanov
Telerik team
commented on 04 May 2021, 09:40 AM

Hello Kim. I can suggest you to check the Missing Controls in the UI help article and see if you can related with the issues described there. If this is not the case, you can open a new support ticket with a sample project showing the issue and attach it in the ticket.
Kim
Top achievements
Rank 1
commented on 09 May 2021, 04:55 PM

OK, thanks, I'll take a look!

No answers yet. Maybe you can help?

Tags
ToolBar
Asked by
Kim
Top achievements
Rank 1
Share this question
or