gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:27:48 26 4
gpt4 key购买 nike

我有一个名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com