当ScrollBar可见时,WPF ScrollViewer显示边框

时间:2011-07-08 05:00:21

标签: wpf wpf-controls binding scrollviewer

我想为我的scrollviewer添加边框。只有在ScrollViewer的ScrollBar可见时(VerticalScrollBarVisibility设置为“Auto”)才会显示此顺序

谢谢你!

2 个答案:

答案 0 :(得分:8)

您可以使用样式和触发器执行此操作,如下所示:

<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>

答案 1 :(得分:3)

使用BindingConverter

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

而不是你的xaml:

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

不要忘记将您的转换器添加到资源!

GL&安培; HF