0

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

这是 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;
            }
        }
    }
}
4

1 回答 1

1

这里有几个问题,第一个很常见,很多人尝试将 a 分配Color给 a Brush,您不能直接这样做,解决此问题的一种方法是将 分配给colora SolidColorbrushBackground

例子:

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

另一个问题是DataContextMyData分配Button.

这是一个例子。

xml:

<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));
        }
    }
}
于 2013-02-22T01:50:21.933 回答