Hi all,
I've been trying to create a custom color picker and have got it working fairly well using some other examples and amending them. Here's the XAML:
<UserControl x:Class="Customcontrols.Colorpicker"
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:sys="clr-namespace:System;assembly=mscorlib" Height="40" Width="200" Name="uccolorpicker"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ObjectDataProvider MethodName="GetType"
ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
<ObjectDataProvider.MethodParameters>
<sys:String>System.Windows.Media.Colors, PresentationCore,
Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<ComboBox x:Name="superCombo"
Validation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTamplate}"
ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
SelectedValuePath="Name"
SelectedValue="{Binding ElementName=uccolorpicker, Path=SelectedColor}" SelectionChanged="superCombo_SelectionChanged">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="wpColours" Width="{Binding ActualWidth,
ElementName=superCombo}" Orientation="Horizontal" ItemHeight="15"
ItemWidth="20" Margin="2">
</WrapPanel>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle x:Name="rect" Fill="{Binding Path=Name}" Height="{Binding ActualHeight,
ElementName=superCombo}" Width="{Binding ActualWidth,
ElementName=superCombo}"
RadiusX="0" RadiusY="0"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</UserControl>
And the code behind:
public partial class Colorpicker : UserControl
{
public Colorpicker()
{
InitializeComponent();
}
public Brush SelectedColor
{
get { return (Brush)GetValue(SelectedColorProperty); }
set { SetValue(SelectedColorProperty, value); }
}
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(Colorpicker), new UIPropertyMetadata(null));
}
Now the problem II can't understand is when I try and set the initial selected value to a color in code behind so that it displays the correct color in "rect". If I do it as follows with a KnownColor enum type it works fine:
superCombo.superCombo.SelectedValue = KnownColor.Red;
But if I try and do that with say this:
superCombo.superCombo.SelectedValue = Colors.Plum;
It doesn't work. The SelectedValue stays as null.
Oooh, I almost forgot this part too:
<Application.Resources>
<ControlTemplate x:Key="ComboBoxValidationErrorTamplate">
<DockPanel>
<Border BorderBrush="Transparent" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Application.Resources>
Any help with this is muchly appreciated. Why can I use KnownColor or SystemColor to set the initial value, but not System.Windows.Media.Color? When the itemssource is a collection of that type?
Regards,
Jib