如何在WPF / XAML中绑定背景颜色?

时间:2009-12-25 22:52:23

标签: c# wpf xaml binding background

我需要更改为以下代码,以便背景为红色,我尝试过的两种方式都没有:

alt text
(来源:deviantsart.com

XAML:

<Window x:Class="TestBackground88238.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>

        <TextBlock Text="{Binding Message}" Background="{Binding Background}"/>

        <TextBlock Text="{Binding Message}">
            <TextBlock.Background>
                <SolidColorBrush Color="{Binding Background}"/>
            </TextBlock.Background>
        </TextBlock>

    </StackPanel>
</Window>

代码背后:

using System.Windows;
using System.ComponentModel;

namespace TestBackground88238
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {

        #region ViewModelProperty: Background
        private string _background;
        public string Background
        {
            get
            {
                return _background;
            }

            set
            {
                _background = value;
                OnPropertyChanged("Background");
            }
        }
        #endregion

        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion



        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Background = "Red";
            Message = "This is the title, the background should be " + Background + ".";

        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}

更新1:

我试过Aviad的答案似乎没有用。我可以使用x:Name手动执行此操作,如此处所示,但我希望能够将颜色绑定到INotifyPropertyChanged属性,我该怎么做?

alt text
(来源:deviantsart.com

XAML:

<Window x:Class="TestBackground88238.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>

        <TextBlock Text="{Binding Message}" Background="{Binding Background}"/>

        <TextBlock x:Name="Message2" Text="This one is manually orange."/>

    </StackPanel>
</Window>

代码背后:

using System.Windows;
using System.ComponentModel;
using System.Windows.Media;

namespace TestBackground88238
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {

        #region ViewModelProperty: Background
        private Brush _background;
        public Brush Background
        {
            get
            {
                return _background;
            }

            set
            {
                _background = value;
                OnPropertyChanged("Background");
            }
        }
        #endregion

        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Background = new SolidColorBrush(Colors.Red);
            Message = "This is the title, the background should be " + Background + ".";

            Message2.Background = new SolidColorBrush(Colors.Orange);

        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}

8 个答案:

答案 0 :(得分:50)

重要提示:

确保您使用的是System.Windows.Media.Brush而不是System.Drawing.Brush

它们不兼容,你会遇到绑定错误。

您需要使用的颜色枚举也不同

  System.Windows.Media.Colors.Aquamarine(类名是Colors)&lt; ---使用这一个      System.Drawing.Color.Aquamarine(类名为Color

如果有疑问,请使用Snoop并检查元素的background属性以查找绑定错误 - 或者只查看调试日志。

答案 1 :(得分:21)

Background属性需要Brush个对象,而不是字符串。将属性的类型更改为Brush并对其进行初始化:

Background = new SolidColorBrush(Colors.Red);

答案 2 :(得分:7)

这里有一个复制粘贴代码:

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;
        }
    }

答案 3 :(得分:3)

我想出来了,这只是一个命名冲突问题:如果你使用 TheBackground 而不是背景,它就像在第一个例子。属性Background干扰了Window属性背景。

答案 4 :(得分:3)

我建议阅读以下有关调试数据绑定的博文:http://beacosta.com/blog/?p=52

对于这个具体问题:如果查看编译器警告,您会注意到您的属性一直隐藏了Window.Background属性(或Control或属性定义的任何类)。

答案 5 :(得分:2)

xaml代码:

<Grid x:Name="Message2">
   <TextBlock Text="This one is manually orange."/>
</Grid>

c#代码:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        CreateNewColorBrush();
    }

    private void CreateNewColorBrush()
    {

        SolidColorBrush my_brush = new SolidColorBrush(Color.FromArgb(255, 255, 215, 0));
        Message2.Background = my_brush;

    }

这个适用于Windows 8商店应用。试试看。祝你好运!

答案 6 :(得分:0)

您仍然可以使用“背景”作为属性名称,只要您为窗口指定名称并在绑定的“源”上使用此名称即可。

答案 7 :(得分:0)

您为字符串分配了“红色”。您的背景属性应为颜色类型:

using System.Windows;
using System.ComponentModel;

namespace TestBackground88238
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {

        #region ViewModelProperty: Background
        private Color _background;
        public Color Background
        {
            get
            {
                return _background;
            }

            set
            {
                _background = value;
                OnPropertyChanged("Background");
            }
        }
        #endregion

        //...//
}

然后您可以像这样使用对SolidColorBrush的绑定:

public Window1()
{
    InitializeComponent();
    DataContext = this;

    Background = Colors.Red;
    Message = "This is the title, the background should be " + Background.toString() + ".";

}

不是100%肯定Color-Object上的.toString()方法。它可能会告诉您这是一个颜色等级,但是您会弄清楚这一点的;)

相关问题