开发者

Fill color from property via XAML

I am learning WPF and have this simple question.

How do I set fill color to property vi XAML?

<Rectangle Fill="{Binding Path=BackgroundColorf}" 
           Height="112" Margin="0,84,0,0" VerticalAlignment="Top"开发者_运维知识库 
           Width="116"/>
public partial class MainWindow : Window
{

         /// <summary>
        /// Gets or sets the BackgroundColor.  
        /// </summary>
   public SolidColorBrush BackgroundColorf
   {
       get;
       set;
   }

   public MainWindow()
   {
       this.InitializeComponent();
       BackgroundColorf =  new SolidColorBrush(Colors.Red); 
   }
}


Set the datacontext like this

public MainWindow()
   {
       this.DataContext = this;      
       this.InitializeComponent();
       BackgroundColorf =  new SolidColorBrush(Colors.Red); 
   }

This should work.But there is little more to be done for making your wpf app scalable like Notifications,Dependency properties etc.I recommend you go through the basics of wpf DataBinding architecture before continuing.Go through the link posted by H.B in the comments


To get you going ...

Add a name to Rectangle

<Rectangle x:Name="MyRect" Fill="{Binding Path=BackgroundColorf}" Height="112" ...

then in the code

 InitializeComponent();
 MyRect.DataContext = this;
 BackgroundColorf = new SolidColorBrush(Colors.Red);

Not the best way of doing things - but at least you'll have a red rectangle :)


if you add this your example will work

public MainWindow()
{
   this.InitializeComponent();
   this.DataContext =  this;
   BackgroundColorf =  new SolidColorBrush(Colors.Red); 
}

but you should really in some wpf books or websites to get the basics.

a very good Book is "WPF 4 Unleashed" from Adam Nathan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜