使用WPF将可见性绑定到不同类中的控件

时间:2009-09-11 15:59:57

标签: wpf binding visibility

在我的主窗口xaml中,我有两个用户控件和两个RadioButton。我希望RadioButton s控制用户控件的Visibility xaml摘录:

    <WpfApp2:ViewTree/>

    <WpfApp2:ViewTab/>

    <RadioButton x:Name="radioButton_Tree" GroupName="View"
                 IsChecked="True"> Tree View </RadioButton>

    <RadioButton x:Name="radioButton_Tab" GroupName="View"
                 IsChecked="False" >Tab View</RadioButton>

在用户控件中,我有这样的东西:

Visibility="{Binding IsChecked, 
                     Converter={StaticResource BooleanToVisibilityConverter}, 
                     ElementName=Window1.radioButton_Tree}" >

在运行时我收到此错误:
Cannot find source for binding with reference 'ElementName=Window1.radioButton_Tab'

我在俯瞰什么?

1 个答案:

答案 0 :(得分:1)

名称Window1不在用户控制的上下文中。

你能使用下面的代码吗?

<WpfApp2:ViewTree Visibility="{Binding IsChecked, 
                  Converter={StaticResource BooleanToVisibilityConverter}, 
                  ElementName=radioButton_Tree}" />

<WpfApp2:ViewTab Visibility="{Binding IsChecked, 
                 Converter={StaticResource BooleanToVisibilityConverter}, 
                 ElementName=radioButton_Tab}" />

<RadioButton x:Name="radioButton_Tree" GroupName="View"
             IsChecked="True"> Tree View </RadioButton>

<RadioButton x:Name="radioButton_Tab" GroupName="View"
             IsChecked="False" >Tab View</RadioButton>