C# WP8将自定义按钮的DependencyProperty绑定到UserControl的另一DependencyProperty无效
假设我有两个UserControl:uc1和uc2。uc1派生自按钮,uc2派生自用户控件 uc1有一个DependencyProperty isTextShow: C: XAML: XAML: 我预测在这之后,uc1中的TextBlock将不会显示,但结果是它仍然可见。 我在一些重要的地方设置了断点,发现IsWhather变为False,但绑定到它的uc1的isTextShow仍然为True。这个装订好像有问题 此外,我还更改了这一行:C# WP8将自定义按钮的DependencyProperty绑定到UserControl的另一DependencyProperty无效,c#,windows-phone-7,data-binding,windows-phone-8,C#,Windows Phone 7,Data Binding,Windows Phone 8,假设我有两个UserControl:uc1和uc2。uc1派生自按钮,uc2派生自用户控件 uc1有一个DependencyProperty isTextShow: C: XAML: XAML: 我预测在这之后,uc1中的TextBlock将不会显示,但结果是它仍然可见。 我在一些重要的地方设置了断点,发现IsWhather变为False,但绑定到它的uc1的isTextShow仍然为True。这个装订好像有问题 此外,我还更改了这一行: isTextShow="{Binding Element
isTextShow="{Binding ElementName=UCBase, Path=isWhatEver}"
到
这一次,uc1中的文本块没有出现。因此,这意味着第一层绑定工作正常,但第二层将UserControl的DependencyProperty one绑定到另一层UserControl的DependencyProperty two不工作
有什么重要的事情我忽略了吗?多谢各位
更新:
我想现在我知道了原因,但我仍然不知道解决办法。
原因是:uc1源于按钮;如果我将uc1更改为从UserControl派生,它就可以正常工作!但对于目前的情况,我确实需要将uc1作为Button的子级,所以有什么想法吗?如果您希望将DP绑定到Button IsTextShow自身,则必须将DataContext设置为自身,或者从使用UserControl样式改为自定义控件样式,并使用TemplateBinding到propertyHi@ShawnKendrot感谢您的回复。我使用Visibility={Binding ElementName=ButtonBase,Path=isTextShow,Converter={StaticResource BoolToVisibilityConverter}}来表示ElementName已经存在了,它是否等同于set DataContext本身?你能提供一些更详细的信息吗。Thx.啊,对不起,没有看到ElementName。页面上有很多这样的代码吗?上面的代码只是一个演示。我有几个DP应该以这种方式绑定。您好@ShawnKendrot见上文,我已经更新了我的问题。你对此有什么想法吗?非常感谢。
<Button x:Class="xx.xx.uc1"
....
x:Name="ButtonBase">
<TextBlock
..
Visibility="{Binding ElementName=ButtonBase, Path=isTextShow, Converter="{StaticResource BoolToVisibilityConverter}"}">
</TextBlock>
....
</Button>
public static readonly DependencyProperty isWhatEverProperty = DependencyProperty.Register(
"isWhatEver",
typeof(bool),
typeof(uc2),
new PropertyMetadata(true));
public bool isWhatEver
{
get { return (bool)GetValue(isWhatEverProperty ); }
set { SetValue(isWhatEverProperty , value); }
}
<UserControl x:Class="xx.xx.uc2"
....
x:Name="UCBase">
<uc1
..
isTextShow="{Binding ElementName=UCBase, Path=isWhatEver}">
</uc1>
....
<uc2
....
isWhatEver="False"
/>
isTextShow="{Binding ElementName=UCBase, Path=isWhatEver}"
isTextShow="False",