-2

i am new at wpf and i am trying to learn dependency property. i trying to change the background color of textbox using text from another textbox. i am able to do it using converters but i want to implement it using dependency property. here is the xaml code

<TextBox Name="setbox" Width="150" Height="50" FontWeight="DemiBold" FontSize="25" Canvas.Top="50" Canvas.Left="10"
                Background="{Binding ElementName=statusbar,Path=Text,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource converter1}}"/>

and this my converter code

public class backgroundColourConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo
         culture)
        {
            var backColor = Brushes.Transparent;
            //changes the colour of font to White if the input to the statusbar is "state1"
            if (value != null && value.ToString().Equals("state1"))
            {
                backColor = Brushes.White;
            }
            //changes the colour of font to Lavender if the input to the statusbar is "state2"
            else if (value != null && value.ToString().Equals("state2"))
            {
                backColor = Brushes.Lavender;
            }
            //changes the colour of font to Ivory if the input to the statusbar is "state3"
            else if (value != null && value.ToString().Equals("state3"))
            {
                backColor = Brushes.Ivory;
            }
            //changes the colour of font to Green if the input to the statusbar is "state4"
            else if (value != null && value.ToString().Equals("state4"))
            {
                backColor = Brushes.Green;
            }
            return backColor;
        }



        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

this is working fine buti want to implement this using dependecy property. thanks in advance

jithin
  • 45
  • 8
  • Text is a dependency property. What exactly are you trying to do? – mm8 Nov 01 '17 at 10:48
  • i am trying to change the background color of textbox using a custom made dependency property. – jithin Nov 01 '17 at 10:55
  • Post the your custom made dependency property then. – mm8 Nov 01 '17 at 10:56
  • Here's a good read: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/custom-dependency-properties or this one: https://www.codeproject.com/articles/140620/wpf-tutorial-dependency-property – Kixoka Nov 01 '17 at 11:23
  • what ia actually want to do is create a user control consisting of a label and textbox .and change the background from main window. – jithin Nov 02 '17 at 05:32

1 Answers1

1

If you create your own TextBox control you can add a new property BackgroundColorText and set its value from the other TextBox where you type in the color names. In the setter of BackgroundColorText you then could set the controls background color.

But this is a bit overkill for just modifying the background color. The proper way of doing it is the value converter, imho.

MrToast
  • 1,159
  • 13
  • 41