I hope you have the below requirement,
1) ListBox should make use of scroll bar it its content's size grows bigger than its original.
2) If window is resized, ListBox should grow/shrink along with Window.
I have tried the same with a simple example, please check this and if it differs from your requirement, let me know,
XAML code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="I am in Main Grid"/>
<ListBox Grid.Row="1" BorderBrush="BlueViolet" BorderThickness="5" Margin="10">
<TextBlock Text="I am a ListBox"/>
<Button Content="Add Height and Width of ListBox by 100 pixels" Click="Button_Click"/>
<ListBoxItem Content="ListBoxItem" Background="AliceBlue" Margin="10" BorderBrush="Blue" Width="{Binding ListBoxWidth}" Height="{Binding ListBoxHeight}"/>
</ListBox>
</Grid>
</Window>
C# code:
public partial class MainWindow : Window,INotifyPropertyChanged
{
private int m_ListBoxWidth = 350;
public int ListBoxWidth
{
get { return m_ListBoxWidth; }
set
{
m_ListBoxWidth = value;
OnPropertyChanged("ListBoxWidth");
}
}
private int m_ListBoxHeight = 150;
public int ListBoxHeight
{
get { return m_ListBoxHeight; }
set
{
m_ListBoxHeight = value;
OnPropertyChanged("ListBoxHeight");
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ListBoxWidth += 190;
ListBoxHeight += 140;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}