

If (count > 2 & !string.IsNullOrEmpty(parameters)) If (count > 1 & !string.IsNullOrEmpty(parameters))ĬolorIfFalse = ColorFromName(parameters) 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:
#Colorconverter wpf how to#
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. 309 public static readonly DependencyProperty NumIncreaseVisibleProperty =ģ10 DependencyProperty.Register( " NumIncreaseVisible ", typeof(Visibility), typeof(TimeEditer), new PropertyMetadata(Visibility.I am working on WPF application.I have bound my textblock to my button. This enables animation, styling, binding, etc. 5 DefaultStyleKeyProperty.OverrideMetadata( typeof(TimeEditer), new FrameworkPropertyMetadata( typeof (TimeEditer))) ġ8 var newTime = ( 5 ) Ģ3 24 public override void OnApplyTemplate()Ģ7 btnUp =Template.FindName( " PART_UP ", this) as Button Ģ8 btnDown = Template.FindName( " PART_DOWN ", this) as Button Ģ9 txt1 = Template.FindName( " PART_TXTHOUR ", this) as TextBox ģ0 txt2 = Template.FindName( " PART_TXTMINUTE ", this) as TextBox ģ1 txt3 = Template.FindName( " PART_TXTSECOND ", this) as TextBox ģ2 txt4 = Template.FindName( " PART_TXT4 ", this) as TextBox ģ3 34 35 txt1.GotFocus += TextBox_GotFocus Ĥ4 45 txt4.GotFocus += TextBox2_GotFocus Ĥ7 48 this.GotFocus += UserControl_GotFocus Ĥ9 this.LostFocus += UserControl_LostFocus ĥ0 51 this.Repeater(btnUp, (t, num, reset) =>ĥ7 if (currentTextBox.Name = " PART_TXTHOUR " )ĥ9 int.TryParse(currentTextBox.Text, out int numResult) ĩ6 if (currentTextBox.Name = " PART_TXTHOUR " )ĩ8 int.TryParse(currentTextBox.Text, out int numResult) ġ22 if (numResult callBack, int num, int interval = 500 )ġ72 var timer = new Timer ģ07 308 // Using a DependencyProperty as the backing store for NumIncreaseVisible.
