Color selector

This commit is contained in:
Maddo 2017-03-22 09:20:46 +01:00
commit a3a721af32
6 changed files with 81 additions and 20 deletions

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
namespace WPFCatalog
{
public class FontFamilyStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
FontFamily fontfamily = new FontFamily("Verdana");
if (value != null)
{
fontfamily = new FontFamily(value.ToString());
}
return fontfamily;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View file

@ -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;
}
}
}