Browse Source

ColorPicker: implemented ColorCanvas control. ColorPicker now has an advanced color palette to choose colors form.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
833623716a
  1. 120
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/Implementation/ColorPicker.cs
  2. 176
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/Themes/Generic.xaml
  3. 7
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/ColorUtilities.cs

120
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/Implementation/ColorPicker.cs

@ -13,7 +13,6 @@ namespace Microsoft.Windows.Controls
{
#region Members
//private ToggleButton _colorPickerToggleButton;
private Popup _colorPickerCanvasPopup;
private ListBox _availableColors;
private ListBox _standardColors;
@ -45,29 +44,6 @@ namespace Microsoft.Windows.Controls
#endregion //ButtonStyle
#region DisplayAvailableColors
public static readonly DependencyProperty DisplayAvailableColorsProperty = DependencyProperty.Register("DisplayAvailableColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true, OnDisplayAvailableColorsChanged));
public bool DisplayAvailableColors
{
get { return (bool)GetValue(DisplayAvailableColorsProperty); }
set { SetValue(DisplayAvailableColorsProperty, value); }
}
private static void OnDisplayAvailableColorsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ColorPicker colorPicker = o as ColorPicker;
if (colorPicker != null)
colorPicker.OnDisplayAvailableColorsChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnDisplayAvailableColorsChanged(bool oldValue, bool newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //DisplayAvailableColors
#region DisplayColorAndName
public static readonly DependencyProperty DisplayColorAndNameProperty = DependencyProperty.Register("DisplayColorAndName", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(false));
@ -79,52 +55,6 @@ namespace Microsoft.Windows.Controls
#endregion //DisplayColorAndName
#region DisplayRecentColors
public static readonly DependencyProperty DisplayRecentColorsProperty = DependencyProperty.Register("DisplayRecentColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true, OnDisplayRecentColorsChanged));
public bool DisplayRecentColors
{
get { return (bool)GetValue(DisplayRecentColorsProperty); }
set { SetValue(DisplayRecentColorsProperty, value); }
}
private static void OnDisplayRecentColorsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ColorPicker colorPicker = o as ColorPicker;
if (colorPicker != null)
colorPicker.OnDisplayRecentColorsChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnDisplayRecentColorsChanged(bool oldValue, bool newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //DisplayRecentColors
#region DisplayStandardColors
public static readonly DependencyProperty DisplayStandardColorsProperty = DependencyProperty.Register("DisplayStandardColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true, OnDisplayStandardColorsChanged));
public bool DisplayStandardColors
{
get { return (bool)GetValue(DisplayStandardColorsProperty); }
set { SetValue(DisplayStandardColorsProperty, value); }
}
private static void OnDisplayStandardColorsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ColorPicker colorPicker = o as ColorPicker;
if (colorPicker != null)
colorPicker.OnDisplayStandardColorsChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnDisplayStandardColorsChanged(bool oldValue, bool newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //DisplayStandardColors
#region IsOpen
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(false));
@ -185,6 +115,50 @@ namespace Microsoft.Windows.Controls
#endregion //SelectedColorText
#region ShowAdvancedButton
public static readonly DependencyProperty ShowAdvancedButtonProperty = DependencyProperty.Register("ShowAdvancedButton", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true));
public bool ShowAdvancedButton
{
get { return (bool)GetValue(ShowAdvancedButtonProperty); }
set { SetValue(ShowAdvancedButtonProperty, value); }
}
#endregion //ShowAdvancedButton
#region ShowAvailableColors
public static readonly DependencyProperty ShowAvailableColorsProperty = DependencyProperty.Register("ShowAvailableColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true));
public bool ShowAvailableColors
{
get { return (bool)GetValue(ShowAvailableColorsProperty); }
set { SetValue(ShowAvailableColorsProperty, value); }
}
#endregion //ShowAvailableColors
#region ShowRecentColors
public static readonly DependencyProperty ShowRecentColorsProperty = DependencyProperty.Register("ShowRecentColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(false));
public bool ShowRecentColors
{
get { return (bool)GetValue(ShowRecentColorsProperty); }
set { SetValue(ShowRecentColorsProperty, value); }
}
#endregion //DisplayRecentColors
#region ShowStandardColors
public static readonly DependencyProperty ShowStandardColorsProperty = DependencyProperty.Register("ShowStandardColors", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(true));
public bool ShowStandardColors
{
get { return (bool)GetValue(ShowStandardColorsProperty); }
set { SetValue(ShowStandardColorsProperty, value); }
}
#endregion //DisplayStandardColors
#region StandardColors
public static readonly DependencyProperty StandardColorsProperty = DependencyProperty.Register("StandardColors", typeof(ObservableCollection<ColorItem>), typeof(ColorPicker), new UIPropertyMetadata(CreateStandardColors()));
@ -220,10 +194,8 @@ namespace Microsoft.Windows.Controls
{
base.OnApplyTemplate();
//_colorPickerToggleButton = (ToggleButton)GetTemplateChild("PART_ColorPickerToggleButton");
_colorPickerCanvasPopup = (Popup)GetTemplateChild("PART_ColorPickerPalettePopup");
_colorPickerCanvasPopup.Opened += ColorPickerCanvasPopup_Opened;
//_colorPickerCanvasPopup.Opened += ColorPickerCanvasPopup_Opened;
_availableColors = (ListBox)GetTemplateChild("PART_AvailableColors");
_availableColors.SelectionChanged += Color_SelectionChanged;
@ -241,7 +213,7 @@ namespace Microsoft.Windows.Controls
void ColorPickerCanvasPopup_Opened(object sender, EventArgs e)
{
Mouse.Capture(this, CaptureMode.SubTree);
}
private void OnKeyDown(object sender, KeyEventArgs e)

176
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/Themes/Generic.xaml

@ -15,6 +15,7 @@
<!-- =============================================================================== -->
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<coreConverters:InverseBoolConverter x:Key="InverseBoolConverter" />
<coreConverters:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter" />
<LinearGradientBrush x:Key="ColorPickerDarkBorderBrush" EndPoint="0.5,1" StartPoint="0.5,0">
@ -102,7 +103,13 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border Background="{TemplateBinding Background}" CornerRadius="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<Grid SnapsToDevicePixels="True">
<chrome:ButtonChrome x:Name="ToggleButtonChrome"
RenderChecked="{Binding IsOpen, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ColorPicker}}"
RenderEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ColorPicker}}"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
@ -115,41 +122,7 @@
<Path Width="7" Height="4" Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z" Fill="#FF000000"/>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource ButtonHoverBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonChecked}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource ButtonHoverBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonHover}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource ButtonHoverBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonHover}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="true"/>
<Condition Property="IsChecked" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" Value="{StaticResource ButtonPressedBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonPressed}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsKeyboardFocused" Value="true"/>
<Condition Property="IsChecked" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" Value="{StaticResource ButtonPressedBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonPressed}"/>
</MultiTrigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource ButtonPressedBorder}"/>
<Setter Property="Background" Value="{StaticResource ButtonPressed}"/>
</Trigger>
</ControlTemplate.Triggers>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
@ -173,8 +146,10 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ColorPicker}">
<Grid>
<ToggleButton x:Name="PART_ColorPickerToggleButton" Style="{TemplateBinding ButtonStyle}" MinHeight="22" ClickMode="Press" IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}}" >
<Grid>
<ToggleButton x:Name="PART_ColorPickerToggleButton" IsTabStop="False" MinHeight="22" IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
Style="{TemplateBinding ButtonStyle}">
<Grid Margin="2,2,0,2">
<Border x:Name="ColorOnly" Style="{StaticResource ColorDisplayStyle}" />
<Border x:Name="ColorAndName" Background="White" Visibility="Hidden">
@ -186,79 +161,97 @@
</Grid>
</ToggleButton>
<Popup x:Name="PART_ColorPickerPalettePopup" VerticalAlignment="Bottom" IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" >
<Popup x:Name="PART_ColorPickerPalettePopup" VerticalAlignment="Bottom" IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" StaysOpen="False" AllowsTransparency="True" Focusable="False" HorizontalOffset="1" VerticalOffset="1" >
<Border BorderThickness="1" Background="{StaticResource PopupBackgroundBrush}" BorderBrush="{StaticResource ColorPickerDarkBorderBrush}" Padding="3">
<Grid Margin="4">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Available Colors -->
<Grid Grid.Row="1" Visibility="{TemplateBinding DisplayAvailableColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Available Colors" Background="AliceBlue" Padding="2" Margin="0,0,0,1" />
<ListBox x:Name="PART_AvailableColors" Grid.Row="1" Background="Transparent" BorderThickness="0" SelectionMode="Single"
<Grid x:Name="_gridStandardColorsHost" Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Available Colors -->
<Grid Grid.Row="1" Visibility="{TemplateBinding ShowAvailableColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Available Colors" Background="AliceBlue" Padding="2" Margin="0,0,0,1" />
<ListBox x:Name="PART_AvailableColors" Grid.Row="1" Background="Transparent" BorderThickness="0" SelectionMode="Single"
ItemsSource="{Binding AvailableColors, RelativeSource={RelativeSource TemplatedParent}}"
ItemTemplate="{StaticResource ColorItemTemplate}"
ItemContainerStyle="{StaticResource ColorPaletteLisBoxStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Grid>
</Grid>
<!-- Standard Colors-->
<Grid Grid.Row="2" Visibility="{TemplateBinding DisplayStandardColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Standard Colors" Background="AliceBlue" Padding="2" Margin="0,1,0,1"/>
<ListBox x:Name="PART_StandardColors" Grid.Row="1" SelectionMode="Single" Background="Transparent" BorderThickness="0"
<!-- Standard Colors-->
<Grid Grid.Row="2" Visibility="{TemplateBinding ShowStandardColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Standard Colors" Background="AliceBlue" Padding="2" Margin="0,1,0,1"/>
<ListBox x:Name="PART_StandardColors" Grid.Row="1" SelectionMode="Single" Background="Transparent" BorderThickness="0"
ItemsSource="{Binding StandardColors, RelativeSource={RelativeSource TemplatedParent}}"
ItemTemplate="{StaticResource ColorItemTemplate}"
ItemContainerStyle="{StaticResource ColorPaletteLisBoxStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Grid>
</Grid>
<!-- Recent Colors-->
<Grid Grid.Row="3" Margin="0,1,0,1" Visibility="{TemplateBinding DisplayRecentColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Recent Colors" Background="AliceBlue" Padding="2" Margin="0,1,0,1"/>
<ListBox x:Name="PART_RecentColors" Grid.Row="1" SelectionMode="Single" Background="Transparent" BorderThickness="0"
<!-- Recent Colors-->
<Grid Grid.Row="3" Margin="0,1,0,1" Visibility="{TemplateBinding ShowRecentColors, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Recent Colors" Background="AliceBlue" Padding="2" Margin="0,1,0,1"/>
<ListBox x:Name="PART_RecentColors" Grid.Row="1" SelectionMode="Single" Background="Transparent" BorderThickness="0"
ItemsSource="{Binding RecentColors, RelativeSource={RelativeSource TemplatedParent}}"
ItemTemplate="{StaticResource ColorItemTemplate}"
ItemContainerStyle="{StaticResource ColorPaletteLisBoxStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="200" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Grid>
</Grid>
<!-- ColorCanvas -->
<Grid x:Name="_colorCanvasHost" Visibility="Collapsed">
<local:ColorCanvas x:Name="PART_ColorCanvas" Background="Transparent" BorderThickness="0"
SelectedColor="{Binding SelectedColor, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
<Separator Grid.Row="1" HorizontalAlignment="Stretch" Margin="5,0,5,0" />
<!-- More Colors Button -->
<ToggleButton x:Name="_colorMode" Grid.Row="2" Content="Advanced" Margin="5" Visibility="{TemplateBinding ShowAdvancedButton, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Grid>
</Border>
</Popup>
@ -268,6 +261,11 @@
<Setter TargetName="ColorOnly" Property="Visibility" Value="Collapsed" />
<Setter TargetName="ColorAndName" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger SourceName="_colorMode" Property="IsChecked" Value="True">
<Setter TargetName="_colorMode" Property="Content" Value="Standard" />
<Setter TargetName="_colorCanvasHost" Property="Visibility" Value="Visible" />
<Setter TargetName="_gridStandardColorsHost" Property="Visibility" Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>

7
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/ColorUtilities.cs

@ -13,7 +13,12 @@ namespace Microsoft.Windows.Controls.Core.Utilities
public static string GetColorName(this Color color)
{
return KnownColors.Where(kvp => kvp.Value.Equals(color)).Select(kvp => kvp.Key).FirstOrDefault();
string colorName = KnownColors.Where(kvp => kvp.Value.Equals(color)).Select(kvp => kvp.Key).FirstOrDefault();
if (String.IsNullOrEmpty(colorName))
colorName = color.ToString();
return colorName;
}
private static Dictionary<string, Color> GetKnownColors()

Loading…
Cancel
Save