silverlight - 自定义 UserControl 属性未通过 Silverlight 4 中的 XAML DataBinding 设置

标签 silverlight data-binding xaml user-controls silverlight-4.0

我有一个名为 GoalProgressControl 的自定义用户控件。另一个用户控件包含 GoalProgressControl 并通过 XAML 中的数据绑定(bind)设置其 GoalName 属性。但是,GoalName 属性永远不会设置。当我在 Debug模式下检查它时,GoalName 在控件的生命周期内保持为“null”。

如何设置 GoalName 属性?我是否做错了什么?

我正在使用 .NET Framework 4 和 Silverlight 4。我对 XAML 和 Silverlight 还比较陌生,因此我们将不胜感激。

我尝试将 GoalProgressControl.GoalName 更改为 POCO 属性,但这会导致 Silverlight 错误,并且我的阅读使我相信数据绑定(bind)属性应该是 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”项。如果我在 TextBlock 中进行注释并注释掉 GoalProgressControl,则绑定(bind)可以正常工作,并且 TextBlock 可以正确显示 GoalName。另外,如果我将上面的“GoalName”属性替换为简单的文本字符串(例如“hello world”),则控件将正确呈现,并且在呈现时控件上会显示“hello world”。

最佳答案

你必须通过

    new PropertyMetadata(OnValueChanged)

作为 DependencyProperty.Register 调用的最后一个参数并设置属性

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

关于silverlight - 自定义 UserControl 属性未通过 Silverlight 4 中的 XAML DataBinding 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2775659/

相关文章:

wpf - 缩放控件到 WPF 表单

silverlight - 使用 XAML 和文本 Text ="Some text {Some binding} some more text}"进行内联绑定(bind)的最佳实践

wpf - 将 UserControl 的 datacontext 设置为在父 View 模型中定义的 ViewModel

wpf - 绑定(bind)到表达式

wpf - 扳机未触发

javascript - 如何在 Netflix (mac) 中自动化 "next episode"按钮?

c# - 是否可以在 Windows Phone 7 文本框中放置 "hints"?

android - Gradle : No resource type specified (at 'text' with value '@{user.name}' ). 数据绑定(bind)不工作

c# - 移动列表框项目而不是 "selector bar"

c# - 在 WPF 项目中调用 Winforms ControlPaint.Light()