CategoryTemplate in ColorPickerGallery

Ribbon for WPF Forum

Posted 13 years ago by Tim C
Version: 11.1.0542
Avatar
Hi,

I was wondering if it is possible to have different DataTemplates for the different Categories in a ColorPickerGallery (when in categorized mode), or to have a specific DataTemplate for one specific item in such a category, and if so, how I would assign this in code.

If someone has a small sample of how to do this, it would be greatly appreciated.

Thanks,
Tim

Comments (4)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Tim,

Since galleries are ItemsControls, you should be able to use an ItemTemplateSelector. Perhaps you could examine the item's category there and pick an appropriate DataTemplate based on that. We don't have a sample of this though but you should be able to find ItemTemplateSelector assistance by searching the web for it.


Actipro Software Support

Posted 13 years ago by Tim C
Avatar
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
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Tim,

We set ItemTemplate in our default ColorPickerGallery Style so you'd need to clear that first with a null. It should work after that:
<ribbon:ColorPickerGallery x:Name="MyGallery" ItemTemplateSelector="{StaticResource MyTemplateSelector}" ItemTemplate="{x:Null}" />


Actipro Software Support

Posted 13 years ago by Tim C
Avatar
It works now, thanks!
The latest build of this product (v24.1.1) was released 21 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.