繁体   English   中英

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

[英]Resizing the size of the listbox according to the C# wpf window size

我已经使用Visual Studio 2012中的mahapps制作了一个C#Metro WPF应用程序。我已经在窗口中添加了一个列表框。

在此处输入图片说明

但是当我调整窗口大小时,它保持不变。

在此处输入图片说明

这是我当前的代码。

<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>

列表框的底部应位于窗口的底部(始终保持相同的小间距)。 我该如何实现? 我应该如何更改代码?

干得好

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

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

如果您需要根据窗口的宽度调整列表框的宽度,则也可以删除HorizontalAlignmentWidth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM