How can I access DependencyProperty values ​​from my code constructor?

I have a UserControl named SmartForm that has a DependencyProperty called Status .

In my Window1.xaml, I have a <local:SmartForm Status="Ready"/> element.

I would then think in the constructor of the SmartForm object that Status would be "Ready", but instead it would be zero.

Why then is the value of the Status NULL property in the SmartForm constructor?

If not in the UserControl constructor , when I have access to the value , then?

Window1.xaml:

 <Window x:Class="TestPropertyDefine23282.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestPropertyDefine23282" Title="Window1" Height="300" Width="300"> <Grid> <local:SmartForm Status="Ready"/> </Grid> </Window> 

SmartForm.xaml:

 <UserControl x:Class="TestPropertyDefine23282.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <TextBlock x:Name="TestingMessage"/> </Grid> </UserControl> 

SmartForm.xaml.cs:

 using System.Windows; using System.Windows.Controls; namespace TestPropertyDefine23282 { public partial class SmartForm : UserControl { public SmartForm() { InitializeComponent(); TestingMessage.Text = Status; //WHY IS STATUS NOT YET SET HERE? } #region DependencyProperty: Status public string Status { get { return (string)GetValue(StatusProperty); } set { SetValue(StatusProperty, value); } } public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(string), typeof(SmartForm), new FrameworkPropertyMetadata()); #endregion } } 
+4
source share
4 answers

You can set this test message as:

 ... public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(string), typeof(SmartForm), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnStatusChanged))); public static void OnStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((SmartForm)d).TestingMessage.Text = e.NewValue.ToString(); } ... 

Or how:

 <UserControl x:Class="TestPropertyDefine23282.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestPropertyDefine23282" Height="300" Width="300" > <Grid> <TextBlock x:Name="TestingMessage" Text="{Binding Path=Status, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:SmartForm}}}" /> </Grid> </UserControl> 
+3
source
 <local:SmartForm Status="Ready"/> 

Translated to:

 SmartForm f = new SmartForm(); f.Status = Status.Ready; 

You will have access to this value when calling the setter.

+3
source

Shimon Rosea perfectly explained the problem. You check the parameter before setting it, but after initializing the constructor.

A good solution uses the loaded event instead like this:

(unverified)

  public SmartForm() { InitializeComponent(); Loaded += (sender, args) => { TestingMessage.Text = Status; }; } 
+1
source

This is a tertiary type, but why do you need this setter at all?

 <UserControl x:Class="TestPropertyDefine23282.SmartForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Control" Height="300" Width="300"> <Grid> <TextBlock Text="{Binding Path=Status, ElementName=Control}" /> </Grid> </UserControl> 
-1
source

All Articles