模板绑定和自定义组件绑定的问题

时间:2011-12-22 02:57:23

标签: windows-phone-7.1 windows-phone-7

我是WP7和.net编程世界的新手,我需要帮助。我有一个自定义组件,其具有使用模板绑定的属性。

<TextBlock Text="{TemplateBinding Info}" FontSize="20" Grid.Row="1" TextWrapping="{TemplateBinding TextWrap}"/>

我在.cs文件中定义了依赖项属性。

现在在我的page.xaml中,我放置了这样的自定义组件,

 <rounded:RoundedImageView x:Name="pivotItem1" Info="Test bind" BorderBrush="White" ImageSrc="Images/default_service.png" TextWrap="Wrap"/>

哪个有效,现在我希望Info和TextWrap属性可以根据一些外部变量动态更改,所以我这样做了

 <rounded:RoundedImageView x:Name="pivotItem1" Info="{Binding sopInfo}" BorderBrush="White" ImageSrc="Images/default_service.png" TextWrap="{Binding wrap}"/>

其中 sopInfo wrap 是在页面的后备cs文件中定义的外部变量。但这不起作用,Info和TextWrap值不会改变。我怎样才能实现它? 感谢

1 个答案:

答案 0 :(得分:1)

尝试像这样设置页面的DataContext:

 <phone:PhoneApplicationPage 
      DataContext="{Binding RelativeSource={RelativeSource Self}}" />

然后确保sopInfowrap是您网页类的公开DependancyProperties

public static readonly DependencyProperty sopInfoProperty = 
    DependencyProperty.Register(
    "sopInfo", typeof(String),
    );

public string sopInfo
{
    get { return (string)GetValue(sopInfoProperty); }
    set { SetValue(sopInfoProperty, value); }
}