Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

iam creating a property with help of dependencyproperty.
public bool IsPurpule
          get { return (bool)GetValue(IsPurpuleProperty); }
          set { SetValue(IsPurpuleProperty, value); }

      // Using a DependencyProperty as the backing store for IsPurpule.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty IsPurpuleProperty =
          DependencyProperty.Register("IsPurpule", typeof(bool), typeof(mystackpanel), new UIPropertyMetadata(false, IsPurplePropertyChanged));

      private static void IsPurplePropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
          mystackpanel stackpanel = dependency as mystackpanel;

          if (stackpanel != null)
                                     //if i want pass color from xaml how to pass the colr insted of brushes.purple
              stackpanel.Background = Brushes.Purple;


//xaml code
                       //here insted of bool variable i want to pass color?  
<local:mystackpanel IsPurpule="True" >


how can i do this let me know?

Thanks in advance
Updated 13-Jul-12 1:03am
markovl 13-Jul-12 4:35am    
This doesn't make much sense. Why not use the StackPanel.Background property to assign a background?
U@007 13-Jul-12 4:38am    
if i want to create mycustomcolor(as a background) property
how can i do?

If you want to achieve similar functionality to the Background property, you could use a Brush type DependencyProperty:

public Brush MyCustomColor
    get { return (Brush)GetValue(MyCustomColorProperty); }
    set { SetValue(MyCustomColorProperty, value); }
public static readonly DependencyProperty MyCustomColorProperty =
    DependencyProperty.Register("MyCustomColor", typeof(Brush), typeof(mystackpanel));
Share this answer
U@007 13-Jul-12 6:03am    
implement onproperty also
markovl 13-Jul-12 6:27am    
Try to do that yourself. Otherwise how do to you expect to learn?
U@007 16-Jul-12 1:47am    
public Brush MyBackground
get { return (Brush)GetValue(MyBackgroundProperty); }
set { SetValue(MyBackgroundProperty, value); }

// Using a DependencyProperty as the backing store for MyBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyBackgroundProperty =
DependencyProperty.Register("MyBackground", typeof(Brush), typeof(mystackpanel),new PropertyMetadata(System.Windows.Media.Brushes.White,MyCustomBackground));

private static void MyCustomBackground(DependencyObject dendencey, DependencyPropertyChangedEventArgs args)
mystackpanel stack = dendencey as mystackpanel;
if (stack != null)
{ //how can i pass here the parameter (color from xaml)
stack.MyBackground = System.Windows.Media.Brushes.Red;
markovl 16-Jul-12 1:59am    
It is not quite clear what you're trying to achieve here.
The MyCustomBackground callback is called when the value of MyBackground changes - if you assign a value to it from XAML it will become the value of MyBackground.

I suggest you read on dependency properties:
- on MSDN:
- a nice series by Josh Smith:
Instead of bool make it as typeof string
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900