C# 如何使用dependency属性更改背景色

C# 如何使用dependency属性更改背景色,c#,wpf,dependency-properties,ivalueconverter,C#,Wpf,Dependency Properties,Ivalueconverter,我是wpf的新手,我正在努力学习依赖属性。 我正在尝试使用另一个文本框中的文本更改文本框的背景色。我可以使用转换器来实现它,但我想使用dependency属性来实现它。 这是xaml代码 <TextBox Name="setbox" Width="150" Height="50" FontWeight="DemiBold" FontSize="25" Canvas.Top="50" Canvas.Left="10" Background="{Binding

我是wpf的新手,我正在努力学习依赖属性。 我正在尝试使用另一个文本框中的文本更改文本框的背景色。我可以使用转换器来实现它,但我想使用dependency属性来实现它。 这是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}}"/>
这工作正常,但我想使用dependecy属性实现它。
提前感谢

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


但是仅仅修改背景颜色就有点过分了。正确的方法是使用值转换器imho。

Text是一个依赖属性。你到底想做什么?我正试图使用自定义的依赖属性更改textbox的背景色。然后发布自定义的依赖属性。下面是一个好消息:或者这个:ia实际上想做的是创建一个由标签和textbox组成的用户控件。然后从主窗口更改背景。
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();
        }
    }