2013-02-22 33 views
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; 
      } 
     } 
    } 
} 

回答

1

有幾個問題在這裏,第一個是很常見的,很多人嘗試分配一個ColorBrush,你不能做到這一點直接,圍繞這一辦法是分配colorSolidColorbrushBackground的。

例子:

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