我希望你有以下要求, 1)列表框應該利用它的內容的大小的增長比原來更大的滾動條。
2)如果窗口被調整大小,ListBox應該隨Window一起增長/縮小。
我試圖用一個簡單的例子是相同的,請檢查這一點,如果它從你的要求不同,讓我知道,
XAML代碼:
<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#代碼:
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));
}
}
}
有同樣的問題,但在一個網格內的高度可以通過GridSplitter進行調整。將項添加到列表框會增加列表框並移動網格分離器而不是滾動。 – 2012-09-07 02:20:43