Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access DependencyProperty values from my code-behind constructor?

I have a UserControl called SmartForm which has a DependencyProperty called Status.

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

I would think then in the constructor of the SmartForm object, that Status would equal "Ready" but instead it equals null.

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

If not in the UserControl constructor, when do 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

    }
}
like image 418
Edward Tanguay Avatar asked May 25 '09 13:05

Edward Tanguay


People also ask

What is custom dependency property in WPF?

Dependency properties are properties that are registered with the WPF property system through Register or RegisterReadOnly calls. The Register method returns a DependencyProperty instance that holds the registered name and characteristics of a dependency property.

Why dependency property is readonly in WPF?

Read-only dependency properties typically report state, and shouldn't be modifiable through a public accessor. For example, the Windows Presentation Foundation (WPF) framework implements the IsMouseOver property as read-only because its value should only be determined by mouse input.

What are attached properties in WPF?

An attached property is a Extensible Application Markup Language (XAML) concept. Attached properties enable extra property/value pairs to be set on any XAML element that derives from DependencyObject, even though the element doesn't define those extra properties in its object model.

What is difference between dependency property and attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.


2 Answers

<local:SmartForm Status="Ready"/>

Translates to:

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

You will have access to that value when the setter is called.

like image 158
Szymon Rozga Avatar answered Nov 15 '22 09:11

Szymon Rozga


You can set that testing 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 as:

<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>
like image 28
Stanislav Kniazev Avatar answered Nov 15 '22 10:11

Stanislav Kniazev