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
<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