2011-07-08 62 views

回答

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 &

+0

它不工作。我的原因是,ScrollViewer的VerticalScrollBarVisibility不會改變。它被設置爲'Auto',並且如果ScrollBar可見,則屬性仍然是自動的。 – rakete

7

爲此,您可以使用樣式和觸發器,像這樣:

<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

這是一個乾淨的方法,但是嘗試將BorderBrush設置爲Transparent或BorderThickness爲0而不是設置Border.Visibility,因爲此代碼片段現在將在不需要滾動條時隱藏整個邊框及其子項(滾動查看器)。 – Viv