我无法使WPF布局正常工作。我希望ListBox通过锚定到窗口底部来垂直拉伸。它目前只是StackPanel
(添加和删除按钮)中控件高度的大小,并调整大小以容纳添加的项目。在WinForms中,我只需将ListView.Anchor
设置为Top|Left|Bottom|Right
,但我不应该过去。我已经尝试了很多方法,例如将它放在DockPanel
中,将所有内容包装在Canvas
等中,但似乎没有任何影响。
这是我的XAML:
<Window x:Class="FileDropAdmin.ViewsTestListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:shared="http://schemas.markpad.net/winfx/xaml/shared"
Title="ViewsTestListView" Height="300" Width="416">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Text="Things:" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="Things" DisplayMemberPath="ThingName" SelectedItem="CurrentThing" Grid.Column="0"/>
<StackPanel Margin="5 0 0 0" VerticalAlignment="Top" Grid.Column="1">
<Button x:Name="AddThing" Content="Add" Margin="0 0 0 0" VerticalAlignment="Top"/>
<Button x:Name="RemoveThing" Content="Remove" Margin="0 5 0 0" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
</Grid>
</Window>
答案 0 :(得分:6)
将你的第二行设置为Height =“*”,如果它是你需要的话,它应该占用窗口的所有空间
答案 1 :(得分:0)
添加占用所有剩余空间的另一行...
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
...然后调整文本块和列表框的行值...
<TextBlock Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Text="Things:" />
<Grid Grid.Row="2">
答案 2 :(得分:0)
您需要以下选项......
你的第二行定义必须是Height="*"
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
并且您的ListBox需要ColumnSpan="2"
<ListBox x:Name="Things"
DisplayMemberPath="ThingName"
SelectedItem="CurrentThing"
Grid.ColumnSpan="2"
Grid.Column="0"/>
另外你说你使用DockPanel ...使用dockpanel它更容易。您只需将其设置为LastChildFill =“True”,并将您的ListBox
添加为dockpanel中的最后一个子项。
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<StackPanel Margin="5 0 0 0" Grid.Column="0"
HorizontalAlignment="Stretch">
<TextBlock Text="Things:"/>
<TextBox HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</StackPanel>
<StackPanel Margin="5 0 0 0" Grid.Column="1">
<Button x:Name="AddThing" Content="Add" Margin="0 0 0 0"/>
<Button x:Name="RemoveThing" Content="Remove"
Margin="0 5 0 0"/>
</StackPanel>
</Grid>
<ListBox x:Name="Things" DisplayMemberPath="ThingName"
SelectedItem="CurrentThing" />
</DockPanel>