【问题标题】:How to set the Control Visibilty of the Second Control depending on the Binding Value of the first如何根据第一个控件的绑定值设置第二个控件的控件可见性
【发布时间】:2013-04-28 19:40:20
【问题描述】:

我有一个自定义 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>

谁能帮我看看如何在这里设置可见性?

【问题讨论】:

    标签: wpf binding wpf-controls visibility


    【解决方案1】:

    所以DisableProviderSelector 设置为 True 时是布尔值,WindowsFormsHost 需要为 Collapsed,ComboBox 需要为 Visible。当 bool 为 false 时反转。

    所以就 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}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多