在Silverlight 4中,通过XAML数据绑定设置的自定义UserControl属性未被设置。

3
我有一个名为GoalProgressControl的自定义用户控件。另一个用户控件包含GoalProgressControl,并通过XAML中的数据绑定设置其GoalName属性。然而,GoalName属性从未被设置。当我在调试模式下检查它时,GoalName在控件的生命周期内始终保持为“null”。
如何设置GoalName属性?我是否做错了什么?
我正在使用.NET Framework 4和Silverlight 4。我相对较新于XAML和Silverlight,所以任何帮助都将不胜感激。
我尝试将GoalProgressControl.GoalName更改为POCO属性,但这会导致Silverlight错误,并且我的阅读表明,数据绑定属性应为DependencyProperty类型。我还简化了代码,只关注GoalName属性(代码如下),但没有成功。
以下是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");
        }
    }
}

我已经将GoalProgressControl放在另一个页面上:

    <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" 项目。如果我取消对GoalProgressControl的注释并注释TextBlock,则绑定将正常工作,并且TextBlock会正确显示GoalName。此外,如果我在上面用简单的文本字符串(例如“hello world”)替换“GoalName”属性,则控件将正确呈现,并且在呈现时控件上显示“hello world”。
2个回答

4

你需要通过

    new PropertyMetadata(OnValueChanged)

作为 DependencyProperty.Register 调用的最后一个参数,并设置属性。
    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((GoalProgressControl)d).GoalName = (String)e.NewValue;
    }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接