Unfortunately, using the ItemTemplateSelector doesn't work. I created a DataTemplateSelector, and I see its constructor is being called, but SelectTemplate is never called for the ColorPickerGallery, whereas a ComboBox that has the same ItemTemplateSelector works correctly.
Here's my code:
ColorPickerTemplateSelector.cs:
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
namespace ColorPickerTest
{
public class ColorPickerItemTemplateSelector : DataTemplateSelector
{
private DataTemplate _template1 = null;
public DataTemplate Template1
{
get { return _template1; }
set { _template1 = value; }
}
private DataTemplate _template2 = null;
public DataTemplate Template2
{
get { return _template2; }
set { _template2 = value; }
}
public ColorPickerItemTemplateSelector()
{
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
Brush brush = item as Brush;
if (brush != null)
{
var cat = brush.GetValue(PopupGallery.CategoryProperty);
if (cat != null)
{
switch (cat.ToString())
{
case "Category1": return Template1;
case "Category2": return Template2;
default: return Template1;
}
}
}
return base.SelectTemplate(item, container);
}
}
}
-------------------------------------------------------------------------------
MainWindow.xaml:
-------------------------------------------------------------------------------
<Window x:Class="ColorPickerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
xmlns:local="clr-namespace:ColorPickerTest"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<DataTemplate x:Key="MyTemplate1">
<Ellipse Height="40" Width="40" Fill="{Binding BindsDirectlyToSource=True}"/>
</DataTemplate>
<DataTemplate x:Key="MyTemplate2">
<Rectangle Height="20" Width="20" Fill="{Binding BindsDirectlyToSource=True}"/>
</DataTemplate>
<local:ColorPickerItemTemplateSelector x:Key="MyTemplateSelector" Template1="{StaticResource MyTemplate1}" Template2="{StaticResource MyTemplate2}"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ribbon:ColorPickerGallery x:Name="MyGallery" ItemTemplateSelector="{StaticResource MyTemplateSelector}"/>
<ComboBox Grid.Column="1" x:Name="MyComboBox" ItemTemplateSelector="{StaticResource MyTemplateSelector}" Height="30"/>
</Grid>
</Window>
-------------------------------------------------------------------------------
MainWindow.xaml.cs:
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
using ActiproSoftware.Windows.Controls.Ribbon.UI;
namespace ColorPickerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Init();
}
private void Init()
{
List<SolidColorBrush> brushes = new List<SolidColorBrush>();
SolidColorBrush brush;
brush = new SolidColorBrush(Colors.Green);
brush.SetValue(PopupGallery.CategoryProperty, "Category1");
brush.SetValue(ColorPickerGallery.LayoutBehaviorProperty, ColorPickerGalleryItemLayoutBehavior.Default);
brush.SetValue(ScreenTipService.ScreenTipHeaderProperty, "Green");
brush.SetValue(System.Windows.Controls.ItemsControl.ItemTemplateSelectorProperty, (DataTemplateSelector)FindResource("MyTemplateSelector"));
brushes.Add(brush);
brush = new SolidColorBrush(Colors.Black);
brush.SetValue(PopupGallery.CategoryProperty, "Category1");
brush.SetValue(ColorPickerGallery.LayoutBehaviorProperty, ColorPickerGalleryItemLayoutBehavior.Default);
brush.SetValue(ScreenTipService.ScreenTipHeaderProperty, "Black");
brushes.Add(brush);
brush = new SolidColorBrush(Colors.Blue);
brush.SetValue(PopupGallery.CategoryProperty, "Category2");
brush.SetValue(ColorPickerGallery.LayoutBehaviorProperty, ColorPickerGalleryItemLayoutBehavior.Default);
brush.SetValue(ScreenTipService.ScreenTipHeaderProperty, "Blue");
brushes.Add(brush);
brush = new SolidColorBrush(Colors.Red);
brush.SetValue(PopupGallery.CategoryProperty, "Category2");
brush.SetValue(ColorPickerGallery.LayoutBehaviorProperty, ColorPickerGalleryItemLayoutBehavior.Default);
brush.SetValue(ScreenTipService.ScreenTipHeaderProperty, "Red");
brushes.Add(brush);
MyGallery.CategorizedItemsSource = brushes;
MyComboBox.ItemsSource = brushes;
}
}
}
-------------------------------------------------------------------------------
The combobox has the correct data template, whereas the ColorPickerGallery doesn't. Am I missing something, or is this a bug?
Tim