Используйте конвертер значений, чтобы количество элементов в ListBox контролировало видимость ScrollViewer .
Конвертер:
public class CountToVisibility : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((int)value > 5)
return ScrollBarVisibility.Visible;
else
return ScrollBarVisibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<Window.Resources>
<local:CountToVisibility x:Key="ctv"/>
</Window.Resources>
...
<ListBox VerticalAlignment="Stretch" Grid.Column="0"
ItemsSource="{Binding Path=Parts, Mode=OneWay}" SelectedIndex="{Binding CurrentPartIndex}"
Height="115" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowTextBrush}}"
ScrollViewer.HorizontalScrollBarVisibility="{Binding Path=Parts.Count,Converter={StaticResource ctv}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Grid.Column="0" Content="{Binding CurrentLabel, Mode=OneWay}" MinWidth="150" Width="auto" VerticalAlignment="Stretch"/>
<Label Grid.Column="1" Content="{Binding ItemNumber, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
<Label Grid.Column="2" Content="{Binding Cut, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
<Label Grid.Column="3" Content="{Binding Material, Mode=OneWay}" MinWidth="100" VerticalAlignment="Stretch"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>