2011-12-22 123 views
1

我是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"/> 

其中工程确定,现在我想的信息和TextWrap属性动态基于一些外部变量,所以我做了这个改变

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

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

回答

1

尝试设置的DataContext你的页面是这样的:

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

然后确保sopInfowrap是公共的Page类的DependancyProperties

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

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

非常感谢!没有其他方法吗?它基本上意味着我必须有各地的依赖属性! – 2011-12-22 06:19:19

+0

不幸的是,没有。这是他应该如何在WPF中完成的。您可以尝试并实现INotifyPropertyChanged,但您仍然需要在每个设置器中调用OnPropertyChanged。发生绑定时,WPF必须具有引发事件的机制,这就是为什么它需要DependancyProperty或INotifyPropertyChanged。 http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx – 2011-12-22 13:44:59