So i've been going through some of my old code from the time i was learning the basics of wpf and i found something intresting.
I had a class in the format below
public class TempClass
{
public bool IsValued { get; set; }
private SolidColorBrush _isValuedColour;
public SolidColorBrush IsValuedColor
get
{
if (this.IsValued)
{
_accepted = new SolidColorBrush(Colors.DarkGreen);
}
else
{
_accepted = new SolidColorBrush(Colors.Transparent);
}
return _accepted;
}
set { _accepted = value; }
}
If you see, I created a SolidColorBrush based off my IsValued Property to return a Color Property. This code was from 2013 and I believe I started learning in this way maybe because of the websites that i was going through to learn xaml
Come 2015 I checked another project and noticed that I started using IValueConverter
I was able to achieve the same without creating a new property.
<Button Foreground="{Binding IsValued, Converter={StaticResource boolConverter}}">
and here is my Implementation.
class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
SolidColorBrush brush = new SolidColorBrush();
if ((bool)value)
{
brush = new SolidColorBrush(Colors.Gold);
}
else
{
brush = new SolidColorBrush(Colors.Transparent);
}
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
While both does the job for me, i would like to know which route is better. Is creating new property with getters and setteres a good way or using converters a good way?
Thanks for enlightening me.