Dependency property not updating usercontrol ui

kiz*_*ore 1 silverlight wpf user-controls dependency-properties

I have a usercontrol which has a couple of textblocks on it

<UserControl x:Class="Tester.Messenger"
         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" 
         mc:Ignorable="d" 
         x:Name="myUserControl"
         >  
<TextBlock Text="{Binding ElementName=myUserControl,Path=Header,Mode=TwoWay}" Foreground="LightGray" FontSize="11"  Margin="3,3,0,-3"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding ElementName=myUserControl,Path=Message, Mode=TwoWay}" Foreground="White" FontSize="16" Margin="3,-5"/>
Run Code Online (Sandbox Code Playgroud)

In my code behind I have two dependency properties that I'm binding the above textblocks Text property to.

public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("HeaderProperty", typeof(string), typeof(UserControl), new PropertyMetadata("header"));

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("MessageProperty", typeof(string), typeof(UserControl), new PropertyMetadata(null));


 public string Header
    {
        get
        {
            return (string)GetValue(HeaderProperty);
        }
        set
        {
            SetValue(HeaderProperty, value);
        }
    }

    public string Message
    {
        get
        {
            return (string)GetValue(MessageProperty);
        }
        set
        {
            SetValue(MessageProperty, value);
        }
    }
Run Code Online (Sandbox Code Playgroud)

When I create a object of my UserControl and I change the Header and Message properties and place the control in an ItemControls items collection, then these aren't being reflected in the control. The control just displays the default values for the Header and Message.

 Messenger m = new Messenger();
        m.Header = "colin";
        m.Message = "Download File ?";
        iControl.Items.Add(m);
Run Code Online (Sandbox Code Playgroud)

F R*_*ell 5

您调用中的第一个参数DependencyProperty.Register不正确:

public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("HeaderProperty", typeof(string), typeof(UserControl), new PropertyMetadata("header"));
Run Code Online (Sandbox Code Playgroud)

应该:

public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string), typeof(UserControl), new PropertyMetadata("header"));
Run Code Online (Sandbox Code Playgroud)

该字符串应该是属性的名称,因为它将出现在 XAML 中,即没有“属性”后缀。您的消息也是如此DependencyProperty