Browse Source

ColorPicker: added new property that allows you to specify how to show the color information. You can show just the color or the color and color name. Just set the DisplayColorAndName property to true to display the color block and the name of the color next to it.

<extToolkit:ColorPicker DisplayColorAndName="True"/>
pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
17e1e9f05f
  1. 45
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/ColorPicker.cs
  2. 8
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/ColorUtilities.cs
  3. 33
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

45
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/ColorPicker.cs

@ -13,11 +13,11 @@ namespace Microsoft.Windows.Controls
{
#region Members
//ToggleButton _colorPickerToggleButton;
Popup _colorPickerCanvasPopup;
ListBox _availableColors;
ListBox _standardColors;
ListBox _recentColors;
//private ToggleButton _colorPickerToggleButton;
private Popup _colorPickerCanvasPopup;
private ListBox _availableColors;
private ListBox _standardColors;
private ListBox _recentColors;
#endregion //Members
@ -43,7 +43,18 @@ namespace Microsoft.Windows.Controls
set { SetValue(ButtonStyleProperty, value); }
}
#endregion //ButtonStyle
#endregion //ButtonStyle
#region DisplayColorAndName
public static readonly DependencyProperty DisplayColorAndNameProperty = DependencyProperty.Register("DisplayColorAndName", typeof(bool), typeof(ColorPicker), new UIPropertyMetadata(false));
public bool DisplayColorAndName
{
get { return (bool)GetValue(DisplayColorAndNameProperty); }
set { SetValue(DisplayColorAndNameProperty, value); }
}
#endregion //DisplayColorAndName
#region IsOpen
@ -85,7 +96,7 @@ namespace Microsoft.Windows.Controls
private void OnSelectedColorChanged(Color oldValue, Color newValue)
{
//SelectedColorText = newValue.GetColorName();
SelectedColorText = newValue.GetColorName();
RoutedPropertyChangedEventArgs<Color> args = new RoutedPropertyChangedEventArgs<Color>(oldValue, newValue);
args.RoutedEvent = ColorPicker.SelectedColorChangedEvent;
@ -96,12 +107,12 @@ namespace Microsoft.Windows.Controls
#region SelectedColorText
//public static readonly DependencyProperty SelectedColorTextProperty = DependencyProperty.Register("SelectedColorText", typeof(string), typeof(ColorPicker), new UIPropertyMetadata("Black"));
//public string SelectedColorText
//{
// get { return (string)GetValue(SelectedColorTextProperty); }
// protected set { SetValue(SelectedColorTextProperty, value); }
//}
public static readonly DependencyProperty SelectedColorTextProperty = DependencyProperty.Register("SelectedColorText", typeof(string), typeof(ColorPicker), new UIPropertyMetadata("Black"));
public string SelectedColorText
{
get { return (string)GetValue(SelectedColorTextProperty); }
protected set { SetValue(SelectedColorTextProperty, value); }
}
#endregion //SelectedColorText
@ -247,13 +258,11 @@ namespace Microsoft.Windows.Controls
{
ObservableCollection<ColorItem> _standardColors = new ObservableCollection<ColorItem>();
PropertyInfo[] properties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
foreach (PropertyInfo info in properties)
foreach (var item in ColorUtilities.KnownColors)
{
if (String.Compare(info.Name, "Transparent", false) != 0)
if (String.Compare(item.Key, "Transparent", false) != 0)
{
Color c = (Color)info.GetValue(typeof(Colors), null);
var colorItem = new ColorItem(c, info.Name);
var colorItem = new ColorItem(item.Value, item.Key);
if (!_standardColors.Contains(colorItem))
_standardColors.Add(colorItem);
}

8
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ColorPicker/ColorUtilities.cs

@ -8,12 +8,14 @@ namespace Microsoft.Windows.Controls
{
static class ColorUtilities
{
public static readonly Dictionary<string, Color> KnownColors = GetKnownColors();
public static string GetColorName(this Color color)
{
return _knownColors.Where(kvp => kvp.Value.Equals(color)).Select(kvp => kvp.Key).FirstOrDefault();
return KnownColors.Where(kvp => kvp.Value.Equals(color)).Select(kvp => kvp.Key).FirstOrDefault();
}
static readonly Dictionary<string, Color> _knownColors = GetKnownColors();
static Dictionary<string, Color> GetKnownColors()
private static Dictionary<string, Color> GetKnownColors()
{
var colorProperties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
return colorProperties.ToDictionary(p => p.Name, p => (Color)p.GetValue(null, null));

33
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

@ -645,7 +645,9 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Grid x:Name="arrowGlyph" IsHitTestVisible="False" Grid.Column="1" Margin="5">
<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>
@ -689,7 +691,7 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:ColorPicker}">
<Setter Property="BorderBrush" Value="{StaticResource ColorPickerDarkBorderBrush}" />
<Setter Property="BorderThickness" Value="1" />
@ -700,11 +702,24 @@
<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}}" >
<Border>
<Border.Background>
<SolidColorBrush Color="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border.Background>
</Border>
<Grid>
<Border x:Name="ColorOnly">
<Border.Background>
<SolidColorBrush Color="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border.Background>
</Border>
<Border x:Name="ColorAndName" Background="White" Visibility="Hidden">
<StackPanel Orientation="Horizontal">
<Border HorizontalAlignment="Left" Width="20" Margin="2,1,4,1" >
<Border.Background>
<SolidColorBrush Color="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border.Background>
</Border>
<TextBlock Text="{Binding SelectedColorText, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" />
</StackPanel>
</Border>
</Grid>
</ToggleButton>
<Popup x:Name="PART_ColorPickerPalettePopup" VerticalAlignment="Bottom" IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" >
@ -784,6 +799,12 @@
</Border>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="DisplayColorAndName" Value="True">
<Setter TargetName="ColorOnly" Property="Visibility" Value="Collapsed" />
<Setter TargetName="ColorAndName" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

Loading…
Cancel
Save