Robins Gupta
Robins Gupta

Reputation: 3153

Updating a Custom Control's binded Property on its property change

App.cs

class customRadioButton:RadioButton
{
   private Brush enableColor;
   private Brush disableColor;

   public EnableColor()
   {
       get{ /*get value */}
       set{ /* set value */}
   }
}

Main.xaml

<local:customRadioButton EnableColor={Binding ElementName=disableButton, Path=EnableColor} />
<local:customRadioButton x:Name="disableButton" EnableColor="Red", Path=EnableColor} />

Now I am changing the value of EnableColor dynamically. Problem I am having is that the value assigned but is not getting reflected in main.window

I don't want to use dependency property

Is there any other method to Tell the Binded Elements that its property has changed.
Note:I have tried UpdateTrigger its also not working

Upvotes: 0

Views: 137

Answers (2)

tronious
tronious

Reputation: 1577

Curious what your aversion is to using a dependencyproperty? I answering this on my phone so cant mock one up for you but it would be extremely simple to do.

Just do a Google for "WPF custom control dependency property example" and do exactly what they do just customized for your needs.

The dependency peppery will give you the change notification that you're looking for.

The UpdateTrigger would only apply if you were binding to something that's part of your XAML data context. I mean I guess toy could fudge around to get that to work but DependencyProperty is absolutely the way too go

Upvotes: 0

Manish Basantani
Manish Basantani

Reputation: 17509

In order to get Binding work, you will need to create a DependencyProperty.

  public Brush EnableColor
    {
        get { return (Brush)this.GetValue(EnableColorProperty); }
        set { this.SetValue(EnableColorProperty, value); }
    }
    public static readonly DependencyProperty EnableColorProperty = DependencyProperty.Register(
      "EnableColor", typeof(Brush), typeof(customRadioButton), new PropertyMetadata(default(Brush));

Read MSDN for details.

Upvotes: 2

Related Questions