Choose Brush by name - c#

I have the exact name of a brush (AliceBlue, OrangeRed, etc) and I wonder if it is possible to select the brush by this string. Brushes is a static collection and I don't really know how to do this. I think in a normal collection it could be done by selecting the name property of an item with Linq, but doesn't seem to work here.

Use BrushConverter from the System.ComponentModel namespace:
BrushConverter conv = new BrushConverter();
You can use a color name:
SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;
You can also use an RGB value:
SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;

You could do it with reflection easily enough:
// TODO: Validation :)
Brush brush = (Brush) typeof(Brushes).GetProperty(name)
.GetValue(null);
Alternatively, you could use reflection once to populate a dictionary:
Dictionary<string, Brush> =
typeof(Brushes).GetProperties(BindingFlags.Public |
BindingFlags.Static)
.ToDictionary(p => p.Name,
p => (Brush) p.GetValue(null));

From BrushConverter class (MSDN):
Use this class to convert a string into a SolidColorBrush or an
ImageBrush. See these type pages for syntax information. This class is
typically used by the parser to convert attribute strings to brushes.
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
You can create your own StringToBrushConverter and use above code in its Convert method by passing your string color variables and returning SolidColorBrush variables.

I have two solution for you.
one use A SolidColorBrush property and set it with your desire property like below.
And other one is use convert with BrushConverter Class.
<Window x:Class="WpfApplication1.DynamicSorting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DynamicSorting" Height="329" Width="610">
<Window.Resources>
<local:StringToBrushConverter x:Key="texttobrush"/>
</Window.Resources>
<Grid Background="{Binding SelectedBrush,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="242*" />
<ColumnDefinition Width="346*" />
</Grid.ColumnDefinitions>
<ComboBox Height="35" SelectedItem="{Binding SelectedBrush,Mode=TwoWay}" ItemsSource="{Binding AllBrushes}" HorizontalAlignment="Left" Margin="25,32,0,0" x:Name="comboBox1" VerticalAlignment="Top" Width="143" />
</Grid>
</Window>
public partial class DynamicSorting : Window, INotifyPropertyChanged
{
public DynamicSorting()
{
InitializeComponent();
if (FilesList == null)
FilesList = new ObservableCollection<FileInfo>();
var files = new System.IO.DirectoryInfo("C:\\Windows\\System32\\").GetFiles();
foreach (var item in files)
{
FilesList.Add(item);
}
if (AllBrushes == null)
AllBrushes = new ObservableCollection<string>();
Type t = typeof(Brushes);
var props = t.GetProperties();
foreach (var item in props)
{
AllBrushes.Add(item.Name);
}
this.DataContext = this;
}
private SolidColorBrush _SelectedBrush;
public SolidColorBrush SelectedBrush
{
get { return _SelectedBrush; }
set
{
_SelectedBrush = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectedBrush"));
}
}
public ObservableCollection<FileInfo> FilesList { get; set; }
public ObservableCollection<string> AllBrushes { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public class StringToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return Brushes.Transparent;
var colortext = value.ToString();
var BrushType = typeof(Brushes);
var brush = BrushType.GetProperty(colortext);
if (brush != null)
return brush;
return Brushes.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Related

IMarkupExtension with bindable properties

I've created an IMarkupExtension for an ImageSource which gets a specified symbol from a specified font and displays it in a specified color with a specified height. Most of the times the icon name is static and I write into the XAML directly. But sometimes there are lists of things that have a property which determines which icon should be used. For this case it is necessary that the icon name is bindable.
Here is (more or less) the current state of my FontImageExtension:
[ContentProperty(nameof(IconName))]
public class FontImageExtension : IMarkupExtension<ImageSource>
{
private readonly IconFontService iconFontService;
[TypeConverter(typeof(FontSizeConverter))]
public double Size { get; set; } = 30d;
public string IconName { get; set; }
public Color Color { get; set; }
public string FontFamily { get; set; }
public FontImageExtension()
{
iconFontService = SomeKindOfContainer.Resolve<IconFontService>();
}
public ImageSource ProvideValue(IServiceProvider serviceProvider)
{
if (string.IsNullOrEmpty(IconName))
return null;
IconFont iconFont = iconFontService.GetIconFont();
if (iconFont == null)
return null;
string glyphCode = iconFont.GetGlyphCode(IconName);
if (string.IsNullOrEmpty(glyphCode))
return null;
FontImageSource fontImageSource = new FontImageSource()
{
FontFamily = iconFont.GetPlatformLocation(),
Glyph = glyphCode,
Color = this.Color,
Size = this.Size,
};
return fontImageSource;
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
}
Most of the time I use it like this in XAML (which already works perfectly):
<Image Source="{m:FontImage SomeIcon, Color=Black, Size=48}"/>
But for dynamic UI (e.g. lists or something) I need it like this:
<CollectionView ItemsSource={Binding SomeCollection}">
<CollectionView.ItemTemplate>
<StackLayout>
<Image Source="{m:FontImage IconName={Binding ItemIcon}, Color=Black, Size=48}"/>
<Label Text="{Binding ItemText}"/>
</StackLayout>
</CollectionView.ItemTemplate>
</CollectionView>
How can I make this work?
It seems you could not use IMarkupExtension with bindable properties .As a 'Binding' can only be set on BindableProperty of a BindableObject.The problem is that MarkupExtension class does not derive from BindableObject, that's why it is not possible to set binding on it's properties.Though you let it implement BindableObject,it still could not work.
A workaround is using Value Converters.
For example:
class ImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var p = parameter.ToString().Split('|');
string colorName = p[0];
ColorTypeConverter colorTypeConverter = new ColorTypeConverter();
Color color = (Color)colorTypeConverter.ConvertFromInvariantString(colorName);
double fontSize = double.Parse(p[1]);
//didn't test this here.
IconFontService iconFontService = SomeKindOfContainer.Resolve<IconFontService();
IconFont iconFont = iconFontService.GetIconFont();
if (iconFont == null)
return null;
string glyphCode = iconFont.GetGlyphCode((string)value);
if (string.IsNullOrEmpty(glyphCode))
return null;
FontImageSource fontImageSource = new FontImageSource()
{
FontFamily = iconFont.GetPlatformLocation(),
Glyph = glyphCode,
Color = color,
Size = fontSize,
};
return fontImageSource;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
use in your xaml:
<ContentPage.Resources>
<ResourceDictionary>
<local:ImageSourceConverter x:Key="imageConvert" />
</ResourceDictionary>
</ContentPage.Resources>
<CollectionView ItemsSource={Binding SomeCollection}">
<CollectionView.ItemTemplate>
<StackLayout>
<Image Source="{Binding Name,Converter={StaticResource imageConvert}, ConverterParameter=Color.Black|48}"/>
<Label Text="{Binding ItemText}"/>
</StackLayout>
</CollectionView.ItemTemplate>
</CollectionView>
Also see failed attempt declaring BindableProperty: IMarkupExtension with bindable property does not work and a more ambitious approach to a somewhat different situation - might be relevant: MarkupExtension for binding.
I solved this problem by creating a converter (like #Leo Zhu suggested) but in addition to the IMarkupExtension. So my extension stays as is (with the addition of a constant value that gets used in the converter) and the code for the converter is as follows:
public class FontIconConverter : IValueConverter, IMarkupExtension
{
private IServiceProvider serviceProvider;
public Color Color { get; set; }
[TypeConverter(typeof(FontSizeConverter))]
public double Size { get; set; } = FontIconExtension.DefaultFontSize;
public string FontFamily { get; set; }
public FontIconConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is string iconName))
return null;
var fontIcon = new FontIconExtension()
{
IconName = iconName,
Color = Color,
Size = Size,
FontFamily = FontFamily,
};
return fontIcon.ProvideValue(serviceProvider);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
return this;
}
}
It then can be used like this:
<Image Source="{Binding IconNameProperty, Converter={c:FontIconConverter Color=Black, Size=48}}"/>
And for static values it stays like this:
<Image Source="{m:FontImage SomeIconsName, Color=Black, Size=48}"/>

WPF DataGrid, customizing the display with a converter

I have a unique situation. In one class, I have an inner class that acts as pretty much just a "display" class. In the outer class there is a method called GetDisplayObject that returns a type of the inner class.
I'm trying to bind to a datagrid with the outer class, but by using a converter I'd like to get the correct display. This way I won't have to change a bunch of code in our application and just add a few lines in a couple .xaml files.
I made a little test app that pretty much sums up my problem at it's most basic level. Ideally I'd like to solve the problem by using a converter and returning values only as a display, that way when i'm using the SelectedItem I won't have to change a ton of code that is depending on that certain type(In this case would be the DataObject type).
So here is the objects i'm stuck dealing with
namespace TestApp
{
public class DataObject
{
public class DataObjectDisplay
{
public string ObjectDisplay { get; set; }
}
// props
public int Id { get; set; }
public object Object1 { get; set; }
public object Object2 { get; set; }
// method for getting the display
public DataObjectDisplay GetDisplayObject()
{
DataObjectDisplay display = new DataObjectDisplay();
// logic for determining which object should be displayed
if(Object1 == null)
{
display.ObjectDisplay = "Object1";
}
else
{
display.ObjectDisplay = "Object2";
}
return display;
}
}
}
Here is the Code Behind of my xaml
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace TestApp
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
this.DataObjectCollection = new ObservableCollection<DataObject>();
this.DataObjectCollection.Add(new DataObject() { Id = 1, Object1 = "this", Object2 = "that" });
this.DataObjectCollection.Add(new DataObject() { Id = 1, Object2 = "that" });
this.SelectedItem = new DataObject();
}
private ObservableCollection<DataObject> dataObjectCollection;
private DataObject selectedItem;
public ObservableCollection<DataObject> DataObjectCollection
{
get { return this.dataObjectCollection; }
set
{
dataObjectCollection = value;
OnNotifyPropertyChanged("DataObjectCollection");
}
}
public DataObject SelectedItem
{
get { return this.selectedItem; }
set
{
selectedItem = value;
OnNotifyPropertyChanged("SelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnNotifyPropertyChanged(string property = "")
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
Here is the xaml(This is something like what i'd want to do, using the itemtemplate or something similar, and then a converter to call this GetDisplay function)
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DataObjectToDisplayDataObjectConverter x:Key="ToDisplayConverter"/>
</Window.Resources>
<StackPanel>
<DataGrid ItemsSource="{Binding DataObjectCollection}" SelectedItem="{Binding SelectedItem}" MinHeight="200">
<DataGrid.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Converter={StaticResource ToDisplayConverter}}"/>
</DataTemplate>
</DataGrid.ItemTemplate>
</DataGrid>
</StackPanel>
</Window>
And Finally the converter
using System;
using System.Globalization;
using System.Windows.Data;
namespace TestApp
{
public class DataObjectToDisplayDataObjectConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value.GetType() == typeof(DataObject))
{
DataObject dataObj = (DataObject)value;
dataObj.GetDisplayObject();
return dataObj;
}
return "Invalid Value";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
I am open to suggestions, but this is a small example. In our actual application changing one thing will most likely cascade into a huge ordeal.
If I were you I would have change close coupling of view with ViewModel, Like creating new MainWindowViewModel class & having all properties in it.
Another thing I see GetDisplayObject method, what's the need of calling such a method from converter.
You can re-factor this code & put in the converter something like this.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataObject dataObj = value as DataObject;
if (value == null)
{
return "Invalid Value";
}
if (dataObj.Object1 == null)
{
return "Object1";
}
return "Object2";
}
Luckily since it was a matter of if one object was null and it's related strings were empty, I was able to use Priority Binding with a converter to return DependencyProperty.UnsetValue and force it to use the next binding. I think this was the best solution for the situation.
So the xaml ended up looking like this.
<DataGrid ItemsSource="{Binding DataObjectCollection}" SelectedItem="{Binding SelectedItem}" MinHeight="200">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<PriorityBinding>
<Binding Path="Object1" Converter="{StaticResource EmptyStringToDependencyPropertyUnset}"/>
<Binding Path="Object2"/>
</PriorityBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And the Converter ended up like this
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null || value as string == string.Empty)
{
return DependencyProperty.UnsetValue;
}
return value;
}

C# SolidColorBrush to my Converter Class

i have an object of the type SolidColorBrush and it holds
a SolidColorBrush.
Now i have a converter for my dataGrid which is binded to a list.
Each row in this dataGrid will be colored by the Converter i have.
All is working fine, but how can i return my SolidColorBrush object instead of an static "Brushes.Red" for example.
My converter:
[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value;
switch (errorLevel)
{
case MainWindow.eErrorLevel.Information:
return Brushes.Red;
case MainWindow.eErrorLevel.Warning:
return Brushes.Yellow;
case MainWindow.eErrorLevel.Error:
return Brushes.Red;
}
return Brushes.Gray;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
My converter is not in the MainWindow, if thats important
And My SolidColorBrush object in my MainWindow which is public:
public CurrentColor CurrentColors = new CurrentColor();
public class CurrentColor
{
public SolidColorBrush ERROR { get; set; }
public SolidColorBrush WARNING { get; set; }
public SolidColorBrush INFORMATION { get; set; }
}
EDIT: my brushes can be dynamically set by the user itself
EDIT2: now its working thanks guys :)
Assuming that these colours won't change at runtime, you could declare your brushes as resources above your converter and add properties to your converter for each brush as follows:
Amend your converter to:
[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value;
switch (errorLevel)
{
case MainWindow.eErrorLevel.Information:
return Error;
case MainWindow.eErrorLevel.Warning:
return Warning;
case MainWindow.eErrorLevel.Error:
return Information;
}
return Normal;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
public Brush Normal { get; set; }
public Brush Error { get; set; }
public Brush Warning { get; set; }
public Brush Information { get; set; }
}
Amend your XAML (wherever your converter is added):
<SolidColorBrush x:Key="Normal" Color="#FFAAAAAA"/>
<SolidColorBrush x:Key="Error" Color="#FFFF0000"/>
<SolidColorBrush x:Key="Warning" Color="#FF00FF00"/>
<SolidColorBrush x:Key="Information" Color="#FF0000FF"/>
<local:TypeToColourConverter x:Key="TypeToColourConverter" Normal="{StaticResource Normal}" Error="{StaticResource Error}" Warning="{StaticResource Warning}" Information="{StaticResource Information}" />
This is very 'designer-friendly' (i.e. all these colours can then be changed in Blend) and easy to maintain.
Hope it helps.
Like I said in my comments, here's an example, passing it as converterparameter, there are probably alternatives:
XAML
<Window x:Class="WpfApplicationTestColorConverter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationTestColorConverter"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ErrorColors x:Key="Colors" />
<local:TypeToColourConverter x:Key="ColorConverter" />
</Window.Resources>
<Grid>
<ListBox x:Name="ListBox1" ItemsSource="{Binding MyObjects}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Title}"
Background="{Binding ErrorLevel,
Converter={StaticResource ColorConverter},
ConverterParameter={StaticResource Colors}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Code behide
public partial class MainWindow : Window
{
public ObservableCollection<MyObject> MyObjects { get; } = new ObservableCollection<MyObject>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
// find the (static)resource
var colors = (ErrorColors)FindResource("Colors");
colors.ERROR = new SolidColorBrush(Colors.Red);
colors.WARNING = new SolidColorBrush(Colors.Orange);
colors.INFORMATION = new SolidColorBrush(Colors.Lime);
// Add objects to the list
MyObjects.Add(new MyObject { Title = "This is an error", ErrorLevel = ErrorLevel.Error });
MyObjects.Add(new MyObject { Title = "This is a warning", ErrorLevel = ErrorLevel.Warning });
MyObjects.Add(new MyObject { Title = "This is information", ErrorLevel = ErrorLevel.Information });
}
}
The Converter
[ValueConversion(typeof(ErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (!(value is ErrorLevel))
return Brushes.Gray;
if (!(parameter is ErrorColors))
return Brushes.Gray;
var lvl = (ErrorLevel)value;
var currentColor = (ErrorColors)parameter;
switch (lvl)
{
case ErrorLevel.Information:
return currentColor.INFORMATION;
case ErrorLevel.Warning:
return currentColor.WARNING;
case ErrorLevel.Error:
return currentColor.ERROR;
}
return Brushes.Gray;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
public class ErrorColors
{
public SolidColorBrush ERROR { get; set; }
public SolidColorBrush WARNING { get; set; }
public SolidColorBrush INFORMATION { get; set; }
}
public enum ErrorLevel
{
Error,
Warning,
Information
}
public class MyObject
{
public string Title { get; set; }
public ErrorLevel ErrorLevel { get; set; }
}
Results:

WPF Converter JSON string to multiple textboxes

I've already made a workaround for this problem because of time constraints at work, although I still want to ask for learning purposes.
So I had this issue where I was making an editor screen for some record data, and in this record was a field called 'Quantity'. However, when designed, it was made a quantity placeholder, but it meant different things. So to explain, it is a SkuReference table, that has a 'Type' that defines if it's a 'Quantity per Pack', 'Roll Length', or 'CBC'. Well, for 'Quantity per Pack' and 'Roll Length', a simple number works, however for the 'CBC' (meaning, Corners/Borders/Centers) the data is stored as a JSON string object:
{ 'Corners': 10, 'Borders': 20, 'Centers': 30 }
Now on the WPF screen, if the data is identified as a 'CBC', I route the data to three textboxes, all bound to the 'Quantity' property of the parent object and using a converter and parameters to identify each one and I put the appropriate value into each textbox. Works fine.
The problem I have is when trying to work the ConvertBack part of the converter. I realized that I do not have reference to the original string property that I can edit and supply the new value to, or access to the other textboxes to just rebuild a new string to return. I was trying to come up with a resolution maybe using MultiBinding in my head, but could not completely come through with an answer.
Is this even possible? BTW I ended up just creating new properties that were split up and when the parent object was set parsed and passed around data. However, for future reference it would seem cleaner to me to just use the original data and a converter without the extra work.
Below is other code for reference:
XAML, UpsertSkuReference.Quantity is the JSON string above
<StackPanel Grid.Column="5">
<TextBlock Text="CBC" />
<StackPanel Orientation="Horizontal">
<TextBox Width="30" Text="{Binding UpsertSkuReference.Quantity, ConverterParameter=co, Converter={StaticResource CBCToIndividualConverter}}" IsEnabled="{Binding CBCIsChecked}" />
<TextBox Width="30" Margin="5,0,0,0" Text="{Binding UpsertSkuReference.Quantity, ConverterParameter=b, Converter={StaticResource CBCToIndividualConverter}}" IsEnabled="{Binding CBCIsChecked}" />
<TextBox Width="30" Margin="5,0,0,0" Text="{Binding UpsertSkuReference.Quantity, ConverterParameter=ce, Converter={StaticResource CBCToIndividualConverter}}" IsEnabled="{Binding CBCIsChecked}" />
</StackPanel>
</StackPanel>
Converter
public class CBCToIndividualConverter : IValueConverter
{
JavaScriptSerializer json = new JavaScriptSerializer();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//--value = CBC JSON object string
//--parameter = [co]: Corners, [b]: Borders, [ce]: Centers
if (value != null)
{
if (parameter == null) { throw new Exception("CBCToIndividualConverter: parameter cannot be null"); }
if (new string[] { "co", "b", "ce" }.Contains(parameter.ToString().ToLower()) == false)
{ throw new Exception("CBCToIndividualConverter: parameter must be 'co' for Corners, 'b' for Borders, or 'ce' for Centers"); }
CornerBorderCenterModel cbc = json.Deserialize<CornerBorderCenterModel>(value.ToString());
switch (parameter.ToString().ToLower())
{
case "co": { return cbc.Corners; }
case "b": { return cbc.Borders; }
case "ce": { return cbc.Centers; }
default: { return null; }
}
}
else { return null; }
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
//--value = number for parameter type
//--parameter = [co]: Corners, [b]: Borders, [ce]: Centers
//--?? Uh Oh
}
return null;
}
}
Converters are not supposed to be used like that. There are cleaner ways. I would suggest you a slight bigger refactoring:
Create 3 properties: Borders, Corners and Centers, on the class that contains the string Quantity (SkuReference?);
When you set any of them, update the Quantity; when you update the Quantity, you try to parse in a CornerBorderCenterModel instance and then update the 3 properties with the values of this instance. All this work is doing implementing the OnPropertyChanged method (see my code later);
In the view, bind every TextBox just with the relative property. This way, you need no converter at all (this works if the external DataContext is the instance of UpsertSkuReference; otherwise, you have to set the DataContext of the StackPanel this way: DataContext="{Binding UpsertSkuReference}", so the bindings of the 3 TextBoxes can find the properties on the object.
The whole round:
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace XXX
{
public class CornerBorderCenterModel
{
public int Corners { get; set; }
public int Borders { get; set; }
public int Centers { get; set; }
}
public class SkuReference : INotifyPropertyChanged
{
static JavaScriptSerializer json = new JavaScriptSerializer();
private static string[] _jsonStringParts =
new[] { nameof(Borders), nameof(Corners), nameof(Centers) };
public SkuReference()
{
PropertyChanged += OnPropertyChanged;
}
public int Corners
{
get { return _Corners; }
set
{
if (_Corners != value)
{
_Corners = value;
RaisePropertyChanged();
}
}
}
private int _Corners;
public int Borders
{
get { return _Borders; }
set
{
if (_Borders != value)
{
_Borders = value;
RaisePropertyChanged();
}
}
}
private int _Borders;
public int Centers
{
get { return _Centers; }
set
{
if (_Centers != value)
{
_Centers = value;
RaisePropertyChanged();
}
}
}
private int _Centers;
private void UpdateCBCFromQuantity()
{
//if Quantity is a CBC and is not null do the following:
var cbc = json.Deserialize<CornerBorderCenterModel>(_Quantity.ToString());
if (cbc != null)
{
Corners = cbc.Corners;
Borders = cbc.Borders;
Centers = cbc.Centers;
}
}
public string Quantity
{
get { return _Quantity; }
set
{
if (_Quantity != value)
{
_Quantity = value;
RaisePropertyChanged();
}
}
}
private string _Quantity;
private void UpdateJsonStringFromCBC()
{
Quantity = string.Format(
"{{ 'Corners': {0}, 'Borders': {1}, 'Centers': {2} }}",
_Corners,
_Borders,
_Centers);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (_jsonStringParts.Contains(e.PropertyName))
UpdateJsonStringFromCBC();
else if (e.PropertyName == nameof(Quantity))
UpdateCBCFromQuantity();
}
}
}
<StackPanel Orientation="Horizontal" DataContext="{Binding UpsertSkuReference}">
<TextBox Text="{Binding Corners}" IsEnabled="{Binding CBCIsChecked}" />
<TextBox Text="{Binding Borders}" IsEnabled="{Binding CBCIsChecked}" />
<TextBox Text="{Binding Centers}" IsEnabled="{Binding CBCIsChecked}" />
</StackPanel>
Note that Quantity, Borders, Corners and Centers must raise notifications when they changed (just as I made, or using more advanced tools like the Reactive library), otherwise the whole round can't work.

How to using DynamicResourceExtension and IValueConverter for set color at run-time?

I have an enum :
public enum SignalColor
{
None,
Signal01A,
Signal02A
}
and LightTheme.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--Signal colors-->
<SolidColorBrush x:Key="Signal01ABrush" Color="#8bc34a"/>
<SolidColorBrush x:Key="Signal01BBrush" Color="#5b9914"/>
</ResourceDictionary>
I have SignalColorSelector control for choose color
<c:SignalColorSelector Name="Test" Grid.Column="0" AllowsTransparent="True" SelectedColor="Signal02A"></c:SignalColorSelector>
<c:SignalColorSelector Name="ChooseColor" Grid.Column="1" Header="Choose a color" SelectedColor="Signal01A"></c:SignalColorSelector>
How to create a class SignalColorToBrushConverter which is derived from MarkupExtension and implements IValueConverter ?
In the Convert method, the value should be of type SignalColor.
If it is SignalColor.None, return DependencyProperty.UnsetValue. Otherwise, return the corresponding signal color brush using DynamicResourceExtension.
Edit 1
I create class SignalColorToBrushConverter:
public class SignalColorToBrushConverter : MarkupExtension, IValueConverter
{
private static DynamicResourceExtension dynamicResource = new DynamicResourceExtension();
public SignalColorToBrushConverter()
{
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type tagertType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
SignalColor signalColor = (SignalColor)Enum.Parse(typeof(SignalColor), value.ToString());
switch (signalColor)
{
case SignalColor.None:
return DependencyProperty.UnsetValue;
case SignalColor.Signal01A:
dynamicResource.ResourceKey = "Signal01ABrush";
break;
case SignalColor.Signal01B:
dynamicResource.ResourceKey = "Signal01BBrush";
break;
case SignalColor.Signal02A:
dynamicResource.ResourceKey = "Signal02ABrush";
break;
case SignalColor.Signal02B:
dynamicResource.ResourceKey = "Signal02BBrush";
break;
}
try
{
return Application.Current.FindResource(dynamicResource.ResourceKey);
}
catch (ResourceReferenceKeyNotFoundException)
{
return DependencyProperty.UnsetValue;
}
}
then, using it:
<TextBlock Text="Choose a color" Width="100" FontWeight="Bold" HorizontalAlignment="Left"
Foreground="{Binding Path=SelectedColor, Converter={v:SignalColorToBrushConverter}, ElementName=ChooseColor}"/>
You can create a Markup Extension like below
public class SignalColorExtension : MarkupExtension
{
public SignalColor IncomingSignalColor
{
get;
set;
}
public SignalColorExtension() { }
public SignalColorExtension(string color)
{
SignalColor outColor;
if (! SignalColor.TryParse(color,true,out outColor))
IncomingSignalColor = SignalColor.None;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
var host = target.TargetObject as FrameworkElement;
if (host != null && IncomingSignalColor != SignalColor.None)
{
var colorResourse = host.TryFindResource(IncomingSignalColor.ToString());
return colorResourse;
}
return DependencyProperty.UnsetValue;
}
}
and use find Resource on the TargetObject to find the colors if they exist just like dynamicresource markup. And then you can use your markup like below:
<Grid Background="{sigColor:SignalColor Signal01A}" >
where sigColor is my namespace to the assembly where my markup extension exists.

Categories