Reputation: 6433
I'm setting up a custom property in one of my user controls as you can see below.
C#
public static readonly DependencyProperty RuleType1 = DependencyProperty.Register
(
"RuleType1", typeof(int), typeof(EmailNotification), new PropertyMetadata(0)
);
public int RuleTypeId
{
get { return (int)GetValue(RuleType1); }
set { SetValue(RuleType1, value); }
}
XAML
<helper:VisitationHours RuleTypeId="2" />
This is working great, except, I can't seem to get the RuleTypeId until after the control loads. It always goes to the default value of 0. After the control has loaded, I can access whatever value I set. How can I access this value in the constructor of the control? Thanks
Upvotes: 1
Views: 2002
Reputation: 128060
First, you should fix the name of your dependency property:
public static readonly DependencyProperty RuleTypeIdProperty =
DependencyProperty.Register("RuleTypeId", typeof(int), typeof(EmailNotification), new PropertyMetadata(0));
public int RuleTypeId
{
get { return (int)GetValue(RuleTypeIdProperty); }
set { SetValue(RuleTypeIdProperty, value); }
}
Then, if VisitationHours
is the user control that you want to set the property on, the answer is that you simply can't access the value in the control's contructor. The object is constructed first, then properties are set by WPF.
You might override OnInitialized or attach a handler to the Initialized event for code that must access the property value.
Upvotes: 2