WrapPanel 中的 VerticalAlignment - 如何使其正常工作?

发布于 2024-11-03 10:40:08 字数 2441 浏览 1 评论 0原文

我遇到了一个有点奇怪的问题。

情况是这样的。我有一个 ListView,带有自定义的 GroupStyle 以使数据出现在 Expander 中,其中有一个 WrapPanel code> 包含 StackPanels (其中包含一个 ToggleButton + 一个自定义控件) StackPanel 中的自定义控件默认不可见,但在选中 ToggleButton 时变为可见。 但是,当我检查 ToggleButton 时,会出现自定义控件,并且位于同一行的所有其他控件将移动到垂直中心。 理想情况下,我希望其他成员能够保持领先地位。我尝试在任何可以设置的地方设置 VerticalAlignment="Top" ,但无论如何它都不会改变。

成像问题:

扩展器的初始状态:

Expander before

单击 ToggleButton 后,此处发生的情况是:

Expander after

正如您所看到的,“测试分析​​”按钮已移至中心。我希望它保留在与原来相同的位置。

此外,我已在单独的 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>

列表的 项目模板

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

I am having a kinda strange problem.

Here is the situation. I have a ListView, with a custom defined GroupStyle to make data appear in Expanders, in which there is a WrapPanel containing StackPanels (which contain one ToggleButton + one custom control)
The custom control in the StackPanel is not visible by default, and becomes visible when the ToggleButton is checked.
However, when I check a ToggleButton, the custom control appears, and all the other control situated on the same line will move to a vertical center.
Ideally, I'd like these other members to stay on top. I've tried to set VerticalAlignment="Top" everywhere I could, it doesn't change anyway.

Imaged problem:

The initial state of an expander:

Expander before

Once the ToggleButton is clicked, here is what happens:

Expander after

As you can see, the button "Test Analysis" is moved to stay at the center. I want it to stay in the same place than the original.

Moreover, I've defined all of these styles in separate DataTemplates objects.
Here is some light code (I just removed what's useless for the problem here, won't make you read tons of XAML code :) ):

The expander's content property:

<Expander.Content>
    <WrapPanel Background="White" VerticalAlignment="Top">
        <ItemsPresenter VerticalAlignment="Top"/>
    </WrapPanel>
</Expander.Content>

The list's 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>

The list's 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-11-10 10:40:08

尝试将 VerticalAlignment 属性设置为 Top 或将列表视图的 VerticalContentAlignment 属性设置为 Stretch

   <Style TargetType="ListView">
        <Setter Property="VerticalContentAlignment" Value="Top"/>
    </Style>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>

Try setting the verticalalignment property to Top or the VerticalContentAlignment property of the listview to Stretch

   <Style TargetType="ListView">
        <Setter Property="VerticalContentAlignment" Value="Top"/>
    </Style>

or

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文