UserControl custom property not set using XAML DataBinding in Silverlight 4

I have a user control called GoalProgressControl. Another user control contains a GoalProgressControl and sets the GoalName attribute through data binding in XAML. However, the GoalName property is never set. When I test it in debug mode, GoalName remains "null" for the management resource.

How to set property GoalName? Is there something I'm doing wrong?

I am using the .NET Framework 4 and Silverlight 4. I am relatively new to XAML and Silverlight, so any help would be greatly appreciated.

I tried to change GoalProgressControl.GoalName to a POCO property, but this causes a Silverlight error, and my reading makes me think that the databound properties should be of type DependencyProperty. I also simplified my code to just focus on the GoalName property (code below) without success.

Here is the GoalProgressControl.xaml:

<UserControl x:Class="GoalView.GoalProgressControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Height="100">

<Border Margin="5" Padding="5" BorderBrush="#999" BorderThickness="1">
    <TextBlock Text="{Binding GoalName}"/>
</Border>
</UserControl>

GoalProgressControl.xaml.cs:

public partial class GoalProgressControl : UserControl, INotifyPropertyChanged
{

    public GoalProgressControl()
    {
        InitializeComponent();
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public static DependencyProperty GoalNameProperty = DependencyProperty.Register("GoalName", typeof(string), typeof(GoalProgressControl), null);
    public string GoalName
    {
        get
        {
            return (String)GetValue(GoalProgressControl.GoalNameProperty);
        }
        set
        {
            base.SetValue(GoalProgressControl.GoalNameProperty, value);
            NotifyPropertyChanged("GoalName");
        }
    }
}

I placed GoalProgressControl on another page:

    <Grid Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="5" Background="#eee" Height="200">
        <Border BorderBrush="#999" BorderThickness="1" Background="White">
            <StackPanel>
                <hgc:SectionTitleBar x:Name="ttlGoals" Title="Personal Goals" ImageSource="../Images/check.png" Uri="/Pages/GoalPage.xaml" MoreVisibility="Visible" />
                <ItemsControl ItemsSource="{Binding Path=GoalItems}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <!--TextBlock Text="{Binding Path=[Name]}"/-->
                            <goal:GoalProgressControl GoalName="{Binding Path=[Name]}"/>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </Border>
    </Grid>

"TextBlock" . TextBlock GoalProgressControl, , TextBlock GoalName. , "GoalName" (ex "hello world" ), , "hello world" , .

+3
2

    new PropertyMetadata(OnValueChanged)

DependencyProperty.Register call

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((GoalProgressControl)d).GoalName = (String)e.NewValue;
    }
+4

Source: https://habr.com/ru/post/1744214/


All Articles