如何根据第一个控件的绑定值设置第二个控件的控件可见性

时间:2013-05-04 14:12:35

标签: wpf binding wpf-controls visibility

我有自定义Wpf控件,即combobox:WpfTwComboBox。我想使用名为DisableProviderSelector的属性设置可见性。

通常使用触发器无济于事。这里的场景是当上面的控件,即WindowsFormsHost可见或折叠时,我希望相反的情况发生在下面的自定义控件上。

<StackPanel Grid.Row="3" Grid.Column="2" Height="25" Orientation="Horizontal"     
            Width="375" HorizontalAlignment="Left">
    <WindowsFormsHost Height="25" Width="375">
        <WindowsFormsHost.Style>
            <Style TargetType="WindowsFormsHost">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DisableProviderSelector}" Value="true">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=DisableProviderSelector}" Value="false">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>     
            </Style>
        </WindowsFormsHost.Style>
        <commonControls:ProviderSelectorControl RequiredLevel="Save" ModifiedByUser="providerSelectorControl1_ModifiedByUser" x:Name="providerSelectorControl1"/>
    </WindowsFormsHost>
    <combobox:WpfTwComboBox x:Name="PortalProviderSelector"
                            SelectedValue="{Binding SelectedPortalProvider}"
                            ItemsSource="{Binding Path=PortalProvidersCollection}" 
                            DisplayMemberPath="FullName" Width="350" Height="25"
                            RequiredLevelFlag="Save">
    </combobox:WpfTwComboBox>            
</StackPanel>

有谁可以帮助我如何设置可见性?

1 个答案:

答案 0 :(得分:1)

设置为True时,DisableProviderSelector是bool WindowsFormsHost需要CollapsedComboBox需要Visible。当bool为假时反转。

ComboBox而言,如果bool为True,则为Visible,而当为False则为Collapsed。因此,只需将ComboBox直接绑定到该属性,然后使用BooleantoVisibilityConverter

XAML:

<Window.Resources>
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
...
<combobox:WpfTwComboBox x:Name="PortalProviderSelector"
                        Width="350"
                        Height="25"
                        DisplayMemberPath="FullName"
                        ItemsSource="{Binding Path=PortalProvidersCollection}"
                        RequiredLevelFlag="Save"
                        Visibility="{Binding DisableProviderSelector,
                                            Converter={StaticResource BooleanToVisibilityConverter}}"
                        SelectedValue="{Binding SelectedPortalProvider}" />