Search code examples
c#wpfxaml

Can't make my DependencyProperty work as I expected


I have a very simple User Control which displays a waiting animation with a text above:

<UserControl x:Class="VNegoceNET.Controls.PleaseWait"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:VNegoceNET.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="RootElement" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Grid.RowSpan="3" Background="White" Content="" Opacity="0.8"/>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
                   Grid.Row="0" FontSize="18" Foreground="Black"
                   Margin="8" x:Name="Caption" Text="Loading..."/>
        <local:SpinningWait Grid.Row="1"/>
    </Grid>
</UserControl>

I want to use it like this:

<controls:PleaseWait Text="Jegg Robot"/>

My problem is that it still displays "Loading..." instead of "Jegg Robot", despite my Dependency Property:

public partial class PleaseWait : UserControl
{
    public PleaseWait()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof(String), typeof(PleaseWait), new PropertyMetadata("Loading in progress..."));

    public string Text
    {
        get => (string)this.GetValue(TextProperty);
        set
        {
            Caption.Text = value;
            this.SetValue(TextProperty, value);
        }
    }
}

What I have missed?


Solution

  • WPF doesn't use common property wrappers for DP (public string Text), it uses SetValue() directly, when property is set from xaml (<controls:PleaseWait Text="Jegg Robot"/>). so code in setter isn't invoked.

    What is needed is propertyChangedCallback in metadata:

    public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", typeof(String), typeof(PleaseWait), 
           new PropertyMetadata("Loading in progress...", OnTextChanged));
    
    public string Text
    {
        get => (string)this.GetValue(TextProperty);
        set { this.SetValue(TextProperty, value); }
    }
    
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    {
        var c = (PleaseWait) d;
        c.Caption.Text = c.Text;
    }