簡體   English   中英

如何在WPF / XAML中綁定背景顏色?

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

我需要更改為以下代碼,以便背景為紅色,我嘗試的兩種方式都沒有:

替代文字
(來源: 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屬性,我該怎么做?

替代文字
(來源: 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

    }
}

重要:

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

它們不兼容,你會遇到綁定錯誤。

您需要使用的顏色枚舉也不同

System.Windows.Media.Colors.Aquamarine(類名是Colors )<---使用這一個System.Drawing.Color.Aquamarine(類名是Color

如果有疑問,請使用Snoop並檢查元素的background屬性以查找綁定錯誤 - 或者只查看調試日志。

Background屬性需要Brush對象,而不是字符串。 將屬性的類型更改為Brush並將其初始化為:

Background = new SolidColorBrush(Colors.Red);

在這里你有一個復制粘貼代碼:

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

我想出來了,這只是一個命名沖突問題 :如果你使用TheBackground而不是Background,它就像在第一個例子中發布一樣。 屬性Background干擾了Window屬性背景。

我建議閱讀以下有關調試數據綁定的博文: http//beacosta.com/blog/?p = 52

對於這個具體問題:如果查看編譯器警告,您會注意到您的屬性一直隱藏了Window.Background屬性(或Control或屬性定義的任何類)。

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商店應用程序。 試試看。 祝好運 !

您仍然可以使用“背景”作為屬性名稱,只要您為窗口指定名稱並在綁定的“源”上使用此名稱即可。

您指定了一個字符串“Red”。 您的Background屬性應為Color類型:

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()方法。 它可能會告訴你它是一個Color-Class,但你會把它想象出來;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM