开发者

WPF ScrollViewer show border when ScrollBar is Visible

I w开发者_如何学运维ant to add a border to my ScrollViewer. The border should only be shown when the ScrollBar of the ScrollViewer is Visible (VerticalScrollBarVisibility set to "Auto")

Thank you!


You can do this using styles and triggers, like this:

<Border BorderBrush="Black">
    <Border.Style>
        <Style>
            <Setter Property="Border.Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=theScrollViewer, Path=ComputedVerticalScrollBarVisibility}" Value="Collapsed">
                    <Setter Property="Border.Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <ScrollViewer Name="theScrollViewer">
    </ScrollViewer>
</Border>


Use Binding and Converter.

public sealed class VisibilityToBorderThicknessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            var flag = (Visibility)value;
            if (flag == Visibility.Visible)
                return new Thickness(0);
            else
                return new Thickness(1);
        }
        catch
        {
            return new Thickness(0);
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
          throw new NotImplementedException();
    }
}

and than your xaml:

 <ScrollViewer Name="blah">
      <Border BorderThickness="{Binding ElementName=blah, Path=VerticalScrollBarVisibility , Converter={StaticResources VisibilityToBorder}}">
 </ScrollViewer>

don't forget to add your converter to resources!

GL&HF

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜