Locke
Locke

Reputation: 1165

WPF Controls and DependencyProperty on Custom Controls

I have a C# custom WPF control. I have properties on the control that are based on DependencyProperty's.

public static readonly DependencyProperty CurrentStateProperty = 
 DependencyProperty.Register( "CurrentState", typeof(ControlStateEnum),
 typeof(MyCustomControl), new PropertyMetadata(ControlStateEnum.Started));

public ControlStateEnum CurrentState
{
    get { return (ControlStateEnum) GetValue(CurrentStateProperty); }
    set { SetValue(CurrentStateProperty, value); }
}

Now, if I use the control, and try to use it, ala:

<myControls:MyCustomControl CurrentState="Loaded" />

The CurrentState never gets set to "Loaded" and remains "Started". I want to make it capable of binding, but also capable of being set without binding... Is there something I don't understand or am missing?

When I set a breakpoint on the setter, it doesn't update on the window load.

Upvotes: 0

Views: 440

Answers (2)

Locke
Locke

Reputation: 1165

So, in the end the value was being set on the control from the XAML after the control had been initialized and loaded. So the way to handle it was to add a PropertyChangedHandler on the DependencyProperty.

parapura had a link to the right answer (http://stackoverflow.com/questions/4225373/setters-not-run-on-dependency-properties).

Upvotes: 0

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

Are you sure your not changed the enum somewhere else after the controls get loaded, because that should work as intended

Upvotes: 1

Related Questions