将复选框绑定到ScrollBar.Visibility

时间:2016-04-16 10:48:36

标签: c# wpf xaml

我想将scrollBarVisibilitycheckBox属性IsChecked绑定。 如果IsChecked为真,scrollBarVisibility必须是可见的。

XAML:

<Controls:MetroWindow.Resources>
    <local:Nustatymai x:Key="checkBox_keitejas" />
</Controls:MetroWindow.Resources>
<!-- ..... -->
<ScrollViewer Name="tempimas" IsEnabled="True" VerticalScrollBarVisibility="Disabled" 
              Height="795" HorizontalScrollBarVisibility="{Binding IsChecked, ElementName=check1, 
              Converter={StaticResource checkBox_keitejas}}" >
</ScrollViewer>
<!-- ..... -->
<WrapPanel>
     <TextBlock>Rodyti apatini scrollBar</TextBlock>
     <CheckBox Name="check1" IsChecked="True"/>
</WrapPanel>

C#:

class Nustatymai : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value == true)
            return ScrollBarVisibility.Visible;
        else
            return ScrollBarVisibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
  

现在这段代码对我不起作用,我需要你的帮助来解决它!

2 个答案:

答案 0 :(得分:1)

您不需要转换器来执行此操作。您可以使用DataTrigger:

<ScrollViewer Name="tempimas" IsEnabled="True" VerticalScrollBarVisibility="Visible" 
              Height="100">
                <ScrollViewer.Style>
                    <Style TargetType="ScrollViewer">
                        <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=check1, Path=IsChecked}" Value="True">
                                <Setter Property="HorizontalScrollBarVisibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ScrollViewer.Style>
            </ScrollViewer>
            <WrapPanel>
                <TextBlock>Rodyti apatini scrollBar</TextBlock>
                <CheckBox Name="check1" IsChecked="True"/>
            </WrapPanel>

答案 1 :(得分:0)

您的代码确实有效,我认为您对scrollviewer的高度存在问题。请检查窗户高度。