根据C#wpf窗口大小调整列表框的大小

时间:2014-07-28 14:59:28

标签: c# wpf visual-studio-2012 listbox mahapps.metro

我使用mahapps和Visual Studio 2012制作了一个C#metro wpf应用程序。我在窗口中添加了一个列表框。

enter image description here

但是当我重新调整窗口大小时,它会保持相同的大小。

enter image description here

这是我目前的代码。

<controls:MetroWindow x:Class="My_app.MainWindow"
    WindowStartupLocation="CenterScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="My application" Height="500" Width="1000" EnableDWMDropShadow="True" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Closing="MetroWindow_Closing_1">


<Grid Margin="-1,0,1,0">
    <TextBox controls:TextboxHelper.Watermark="Search here..." controls:TextboxHelper.ClearTextButton="True" x:Name="textBoxSearch" HorizontalAlignment="Left" Height="23" Margin="11,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="255" TextChanged="textBoxSearch_TextChanged" PreviewKeyDown="textBoxSearch_PreviewKeyDown" BorderThickness="0.5"/>
    <Label x:Name="label1" Content="" HorizontalAlignment="Left" Margin="10,47,0,0" VerticalAlignment="Top" Height="17" Width="29"/>
    <ListBox x:Name="listBoxSuggestions" HorizontalAlignment="Left" Height="397" Margin="10,62,0,0" VerticalAlignment="Top" Width="255" SelectionChanged="listBoxSuggestions_SelectionChanged" PreviewKeyDown="listBoxSuggestions_PreviewKeyDown" GotFocus="listBoxSuggestions_GotFocus"/>
    <TextBox x:Name="textBoxResult" HorizontalAlignment="Left" Height="449" Margin="321,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="661" PreviewKeyDown="textBoxResult_PreviewKeyDown" HorizontalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" TextChanged="textBoxResult_TextChanged" BorderThickness="0"/>
</Grid>
</controls:MetroWindow>

列表框的底部应该是窗口的底部(始终保持小的相同间隙)。我怎样才能做到这一点?我该如何更改我的代码?

1 个答案:

答案 0 :(得分:2)

你去吧

从列表框中删除VerticalAlignmentHeight,它会根据窗口的高度调整高度

<ListBox x:Name="listBoxSuggestions" HorizontalAlignment="Left" Margin="10,62,0,0"  Width="255" SelectionChanged="listBoxSuggestions_SelectionChanged" PreviewKeyDown="listBoxSuggestions_PreviewKeyDown" GotFocus="listBoxSuggestions_GotFocus"/>

如果您需要根据窗口宽度调整列表框的宽度,请同时删除HorizontalAlignmentWidth