我遇到了一个奇怪的问题。
情况是这样的。我有一个 ListView
,带有自定义的 GroupStyle
以使数据出现在 Expander
中,其中有一个 WrapPanel
code> 包含 StackPanels
(其中包含一个 ToggleButton + 一个自定义控件)
StackPanel
中的自定义控件默认不可见,但在选中 ToggleButton
时变为可见。
但是,当我检查 ToggleButton
时,会出现自定义控件,并且位于同一行的所有其他控件将移动到垂直中心。
理想情况下,我希望其他成员能够保持领先地位。我尝试在所有可能的地方设置 VerticalAlignment="Top"
,但它无论如何都不会改变。
图像问题:
扩展器的初始状态:
单击ToggleButton
后,会发生以下情况:
如您所见,“测试分析”按钮已移至中央。我希望它保留在与原来相同的位置。
此外,我已在单独的 DataTemplate 对象中定义了所有这些样式。 这是一些简单的代码(我只是删除了对这里的问题无用的代码,不会让您阅读大量的 XAML 代码:) ):
扩展器的内容属性:
<Expander.Content>
<WrapPanel Background="White" VerticalAlignment="Top">
<ItemsPresenter VerticalAlignment="Top"/>
</WrapPanel>
</Expander.Content>
列表的ItemsPanel
:
<ItemsPanelTemplate >
<WrapPanel
Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource
AncestorType=Expander}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
列表的ItemsTemplate
:
<StackPanel Orientation="Vertical" Height="Auto" Width="Auto" VerticalAlignment="Top" >
<ToggleButton BorderBrush="{x:Null}" Background="{x:Null}" IsChecked="{Binding Value.IsToLaunch, Mode=TwoWay}"
Command="{Binding DataContext.UpdateGroupCheckingsCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}">
<Grid Background="Transparent">
<!-- Blah blah blah about the ToggleButton's content -->
</Grid>
</ToggleButton>
<my:UcReleaseChooser>
<!-- Blah blah blah about being visible if ToggleButton is Checked -->
</my:UcReleaseChooser>
</StackPanel>
最佳答案
尝试将 VerticalAlignment 属性设置为 Top 或将 ListView 的 VerticalContentAlignment 属性设置为 Stretch
<Style TargetType="ListView">
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
或
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
关于wpf - WrapPanel 中的 VerticalAlignment - 如何使其正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5802150/