模板绑定和自定义组件绑定问题 [英] issues with template binding and binding of custom component

查看:30
本文介绍了模板绑定和自定义组件绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WP7 和 .net 编程领域的新手,我需要帮助.我有一个自定义组件,它有一个使用模板绑定的属性.

I'm new to the world of WP7 and .net programming for that matter and i need help. I have a custom component that has a property that uses template binding.

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

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

I defined the dependency properties in the .cs file.

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

Now in my page.xaml i placed the custom component like so,

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

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

Which works ok, now I want the Info and TextWrap properties to be changed dynamically based on some external variables so I did this

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

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

where sopInfo and wrap are the external variables defined in the backing cs file of the page. But this doesn't work, the Info and TextWrap values do not change. How can i achieve it? Thanks

推荐答案

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

Try to set the DataContext of your Page like this:

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

然后确保 sopInfowrap 是公开的 页面类的依赖属性.

Then make sure that sopInfo and wrap are public DependancyProperties of your Page class.

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

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

这篇关于模板绑定和自定义组件绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆