From a3a721af325d5eafc157c36525e75c85d82e472e Mon Sep 17 00:00:00 2001 From: Maddo Date: Wed, 22 Mar 2017 09:20:46 +0100 Subject: [PATCH] Color selector --- .../FontFamilyStringConverter.cs | 0 WPFCatalog/Converters/MaddoColorConverter.cs | 59 +++++++++++++++++++ WPFCatalog/MainWindow.xaml | 11 ++-- WPFCatalog/MainWindowViewModel.cs | 2 +- WPFCatalog/WPFCatalog.csproj | 27 +++++---- WPFCatalog/packages.config | 2 +- 6 files changed, 81 insertions(+), 20 deletions(-) rename WPFCatalog/{ => Converters}/FontFamilyStringConverter.cs (100%) create mode 100644 WPFCatalog/Converters/MaddoColorConverter.cs diff --git a/WPFCatalog/FontFamilyStringConverter.cs b/WPFCatalog/Converters/FontFamilyStringConverter.cs similarity index 100% rename from WPFCatalog/FontFamilyStringConverter.cs rename to WPFCatalog/Converters/FontFamilyStringConverter.cs diff --git a/WPFCatalog/Converters/MaddoColorConverter.cs b/WPFCatalog/Converters/MaddoColorConverter.cs new file mode 100644 index 0000000..d26a440 --- /dev/null +++ b/WPFCatalog/Converters/MaddoColorConverter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media; + +namespace WPFCatalog +{ + public class MaddoColorConverter : IValueConverter + { + private DependencyObject _dummy = new DependencyObject(); + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + Color color = Colors.Black; + + if (value != null && value != DependencyProperty.UnsetValue && value is string && !String.IsNullOrWhiteSpace((string)value)) + { + string c = (string)value; + object convertedColor = null; + try + { + convertedColor = ColorConverter.ConvertFromString(c); + } + catch (Exception ex) + { + if (!DesignerProperties.GetIsInDesignMode(_dummy)) + { + throw new FormatException($"String {c} does not represent a valid color", ex); + } + } + if (convertedColor != null) + { + color = (Color)convertedColor; + } + } + + return color; + + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value != null) + { + Color color = (Color)value; + Debug.WriteLine(color.ToString()); + return color.ToString(); + } + return string.Empty; + } + } +} diff --git a/WPFCatalog/MainWindow.xaml b/WPFCatalog/MainWindow.xaml index 9fdb8ae..d13b68f 100644 --- a/WPFCatalog/MainWindow.xaml +++ b/WPFCatalog/MainWindow.xaml @@ -4,6 +4,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wpfCatalog="clr-namespace:WPFCatalog" + xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" mc:Ignorable="d" Title="Image Catalog" Height="466" Width="772" @@ -19,15 +20,14 @@ - + + - - @@ -236,8 +236,9 @@