![]() If (count > 0 & !string.IsNullOrEmpty(parameters))ĬolorIfTrue = ColorFromName(parameters) Var parameters = parameterstring.Split(' ') If (!string.IsNullOrEmpty(parameterstring)) Var parameterstring = parameter.ToString() / A SolidColorBrush in the supplied or default colors depending on the state of value. / A CSV string on the format may be provided for customization, default is. / Bolean value controlling wether to apply color change ![]() #region Implementation of IValueConverter using System Ĭlass BoolToColorBrushConverter : IValueConverter In my case I want the output to be "Transparent" when value is false. I specify "LimeGreen" as a fallback color if the interpertation of ConverterParameter fails. Therfore I made a helper method called ColorFromName(string name). ![]() This type does not have a Color.FromName() method, like the class. Since the SolidColorBrush() method mentioned by is part of the library, it uses the Color type from that same library. I here provide the Converter I taking the parameter. To make this converter general you may use a ConverterParameter for specifying the colors which is to be inserted when value is true or false. This is demonstrated in the second TextBlock which uses a property Trigger to check its own IsEnabled property against the value of true (since its IsEnabled property will be the same as its parent's). That is, if you set IsEnabled to false on your Button, then your TextBlock will have its IsEnabled property updated to false automatically. However, this is overkill - the IsEnabled property is propagated down to children automatically by WPF. In the example above, the first TextBlock binds to its parent's IsEnabled property using a DataTrigger, and sets the Foreground to some colour if it is true. However, do you really need to use a converter? This can be done in XAML only using Triggers: The answer above shows you how to correctly you use a converter. Public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)īutton's enable,isable property changes from viewmodel(e.g using RaiseCanExecuteChanged)()) Public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public class BrushColorConverter : IValueConverter I have declared following class in my "Models" folder. I want to set foreground of my textblock to black color when its associated button's isEnabled is true. I posted this because in the last few months I have had to do this a bit, and kept forgetting how each time I wanted to, so hopefully this will serve as a useful reminder.I am working on WPF application.I have bound my textblock to my button. These methods provide two ways to convert a colour into a brush that you can then use to fill a rectangle or other UI element as necessary using programmatic means. Var brush = new SolidColorBrush(.Goldenrod) Īlternatively, if you have the colour stored in a variable then you can convert it like this: private void ConvertColour()Ĭolor col = .Goldenrod Converting from Ĭonverting from a system colour can be done by an instantiation of the SolidColorBrush type and feeding it the colour as a parameter in the constructor, like so: private void ConvertColour() For a full list of methods available to the brush converter, see the MSDN article here. There are other ways to convert colours to brushes using the brush converter, including from objects, mostly inherited from TypeConverter. Var brush = (Brush)new ().ConvertFromString("#FF0000") Other variations may work but I’ve only tried and tested the one I’m about to show. It’s best if the string represents a colour code in a 6-digit hexadecimal format (“#FF0000” for example). Converting from StringĬonverting a colour to a string can be done using a brush converter. There can be various ways to do this depending on how you have your colour stored, so this post gives a couple of ways to convert a colour into a brush to style a UI element. Something that may be occasionally frustrating is trying to quickly colour a UI element on a WPF window programmatically when you know the colour code, or maybe even have a ‘color’ variable storing the colour for you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |