如何在 WPF/XAML 中绑定背景颜色? [英] How can I bind a background color in WPF/XAML?

查看:38
本文介绍了如何在 WPF/XAML 中绑定背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对以下代码进行哪些更改才能使背景为红色,但我尝试的两种方法均无效:


(来源:
(来源:deviantsart.com)

XAML:

<堆栈面板><TextBlock Text="{Binding Message}" Background="{Binding Background}"/><TextBlock x:Name="Message2" Text="这个是手动橙色的."/></StackPanel></窗口>

背后的代码:

使用 System.Windows;使用 System.ComponentModel;使用 System.Windows.Media;命名空间 TestBackground88238{公共部分类 Window1 : Window, INotifyPropertyChanged{#region ViewModelProperty:背景私人刷机_background;公共画笔背景{得到{返回_背景;}放{_background = 值;OnPropertyChanged("背景");}}#endregion#region ViewModelProperty:消息私人字符串_message;公共字符串消息{得到{返回_消息;}放{_message = 值;OnPropertyChanged("消息");}}#endregion公共窗口 1(){初始化组件();数据上下文 = 这个;背景 = 新的 SolidColorBrush(Colors.Red);Message = "这是标题,背景应该是"+背景+".";Message2.Background = new SolidColorBrush(Colors.Orange);}#region INotifiedProperty 块公共事件 PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){PropertyChangedEventHandler 处理程序 = PropertyChanged;如果(处理程序!= null){handler(this, new PropertyChangedEventArgs(propertyName));}}#endregion}}

解决方案

重要提示:

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

它们不兼容,您会收到绑定错误.

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

<块引用>System.Windows.Media.Colors.Aquamarine(类名是Colors)<---使用这个System.Drawing.Color.Aquamarine(类名是Color)

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

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)

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>

Code Behind:

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

    }
}

Update 1:

I tried Aviad's answer which didn't seem to work. I can do this manually with x:Name as shown here but I want to be able to bind the color to a INotifyPropertyChanged property, how can I do this?


(source: 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>

Code Behind:

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

    }
}

解决方案

Important:

Make sure you're using System.Windows.Media.Brush and not System.Drawing.Brush

They're not compatible and you'll get binding errors.

The color enumeration you need to use is also different

System.Windows.Media.Colors.Aquamarine (class name is Colors) <--- use this one System.Drawing.Color.Aquamarine (class name is Color)

If in doubt use Snoop and inspect the element's background property to look for binding errors - or just look in your debug log.

这篇关于如何在 WPF/XAML 中绑定背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆