简体   繁体   中英

Binding checkbox to an ScrollBar.Visibility

I want to bind scrollBarVisibility with checkBox property IsChecked . If IsChecked is true scrollBarVisibility need to be Visible.

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();
    }
}

For now this code is not working for me and I need your help to solve it!

You don't need converter to do this. You can use 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>

Your code actually works,I think you have an issue with the height of the scrollviewer. please check it with the window height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM