How can I bind a background color in WPF/XAML?

后端 未结 8 1765
情话喂你
情话喂你 2020-12-05 04:01

What do I have to change to the following code so that the background is red, neither of the 2 ways I tried worked:


(source: deviantsart.com)

相关标签:
8条回答
  • 2020-12-05 04:54

    I figured this out, it was just a naming conflict issue: if you use TheBackground instead of Background it works as posted in the first example. The property Background was interfering with the Window property background.

    0 讨论(0)
  • 2020-12-05 04:57

    Here you've got a copy-paste code:

    class NameToBackgroundConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if(value.ToString() == "System")
                {
                    return new SolidColorBrush(System.Windows.Media.Colors.Aqua);
                }else
                {
                    return new SolidColorBrush(System.Windows.Media.Colors.Blue);
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
        }
    
    0 讨论(0)
提交回复
热议问题