使用数据绑定将颜色(作为字符串)传递到按钮的背景

本文关键字:按钮 背景 字符串 数据绑定 颜色 | 更新日期: 2023-09-27 18:00:24

在下面的片段中,我试图将颜色(作为字符串)传递给控件,并使用绑定为按钮的背景分配颜色。但是,它被忽略了。知道出了什么问题吗?

这是XAML:

<Window x:Class="SDKSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SDKSample"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <DockPanel.Resources>
            <local:MyData x:Key="myDataSource"   />
        </DockPanel.Resources>
        <DockPanel.DataContext>
            <Binding Source="{StaticResource myDataSource}" />
        </DockPanel.DataContext>
        <!--<Button Background="Red" Width="250" Height="25">RED</Button>-->
        <Button Background="{Binding Source={StaticResource myDataSource}, Path=ColorName}" Width="150" Height="30">I'm bound to be red</Button>
    </DockPanel>        
</Window>

这是背后的代码:

namespace SDKSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyData md = new MyData("Red");
            this.DataContext = md.ColorName;
        }
    }
    public class MyData
    {
        private Color colorname;
        public MyData()
        {
        }
        public MyData(string value)
        {
            Color col = (Color)ColorConverter.ConvertFromString(value);
            this.colorname = col;
        }
        public Color ColorName
        {
            get { return colorname; }
            set
            {
                this.colorname = value;
            }
        }
    }
}

使用数据绑定将颜色(作为字符串)传递到按钮的背景

这里有几个问题,第一个问题很常见,很多人试图将Color分配给Brush,你不能直接这样做,解决这个问题的一种方法是将color分配给BackgroundSolidColorbrush

示例:

<Button Content="I'm bound to be red" Width="150" Height="30">
    <Button.Background>
        <SolidColorBrush Color="{Binding ElementName=UI,Path=MyData.ColorName}" />
    </Button.Background>
</Button>

另一个问题是分配DataContext的方式,您真正需要做的就是在窗口上创建一个MyData属性,并将其分配给Button

下面是一个例子。

Xaml:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="124" Width="464" Name="UI" >
        <DockPanel DataContext="{Binding ElementName=UI}"> <!--set the DataContext to your Window (using the Name of the Window)-->
            <Button Content="I'm bound to be red" Width="150" Height="30">
                <Button.Background>
                    <SolidColorBrush Color="{Binding MyData.ColorName}" />
                </Button.Background>
            </Button>
        </DockPanel>
</Window>

代码

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private MyData _myData;
    public MainWindow()
    { 
        InitializeComponent();
        MyData = new MyData("Red");
    }
    public MyData MyData
    {
        get { return _myData; }
        set { _myData = value; NotifyPropertyChanged("MyData"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}