如何使用依赖属性更改背景颜色

时间:2021-06-25 19:43:08

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

我是wpf的新手,我正在努力学习依赖属性。我试图使用另一个文本框中的文本更改文本框的背景颜色。我能够使用转换器,但我想使用依赖属性实现它。这是xaml代码

<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

这是正常工作buti希望使用dependecy属性实现这一点。提前致谢

1 个解决方案

#1


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.

如果您创建自己的TextBox控件,则可以添加新属性BackgroundColorText,并从您键入颜色名称的其他TextBox中设置其值。在BackgroundColorText的setter中,您可以设置控件背景颜色。

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

但仅仅修改背景颜色有点过分。这样做的正确方法是价值转换器,imho。

#1


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.

如果您创建自己的TextBox控件,则可以添加新属性BackgroundColorText,并从您键入颜色名称的其他TextBox中设置其值。在BackgroundColorText的setter中,您可以设置控件背景颜色。

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

但仅仅修改背景颜色有点过分。这样做的正确方法是价值转换器,imho。