gpt4 book ai didi

WPF ContentControl 宽度会增加,但在包裹在 ScrollViewer 中时不会缩小

转载 作者:行者123 更新时间:2023-12-04 15:11:35 25 4
gpt4 key购买 nike

我想弄清楚如何制作我的ContentControl正确地水平滚动(目前垂直滚动)。正确地说,我的意思是我希望看到要拉伸(stretch)(无限扩展)的内容,同时具有滚动条出现的最小尺寸,以便内容不会在 ContentControl 后面溢出。的区域,所以这里简单介绍一下:

主窗口的结构如下:

  • 网格(2 列 .3* 和 .7*)
  • 边界
  • 网格(7 行,一组设置为 * ContentControl 所在的位置)
  • 带有 StackPanel 的 ScrollViewer(纯粹用于测试)包装了具有 Auto Width
  • 的 ContentControl
    ContentControl的模板:
  • 网格(宽度设置为 UserControl 的 ActualWidth,6 行,其中一个设置为 Auto,其中 ItemsControl 去
  • ItemsControl 描述了 DataTemplate 类型的 ItemTemplate,其中包含一个 Grid,我在其中有一个 DataGrid

  • 实际问题是 ContentControl 会随着您调整窗口大小而增长,但不会随着窗口大小调整而缩小。

    主视图 XAML(为清楚起见截断):
    <ScrollViewer Grid.Row="5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <ContentControl Grid.Row="5" Background="Transparent" Focusable="False" Margin="0,5,0,0"
    Content="{Binding CurrentSection}" ContentTemplateSelector="{StaticResource templateSelector}/>
    </ScrollViewer>

    Tempate XAML(为清楚起见截断):
    <Grid>
    ...
    <ItemsControl Grid.Row="4" ItemsSource="{Binding Data.QualifyingDistributionsDividends}" x:Name="QualifyingItemsControl">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <Grid x:Name="DTLayoutGrid">
    ...
    <Grid Grid.Row="1" x:Name="DataLayout" Width="{Binding ElementName=DTLayoutGrid, Path=ActualWidth}" HorizontalAlignment="Stretch">
    ...
    <DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="8" HorizontalScrollBarVisibility="Disabled"
    ItemsSource="{Binding Payments}" Style="{StaticResource DataGridStyle}" CellStyle="{StaticResource DataGridNormalCellStyle}">
    </DataGrid>
    </Grid>
    </Grid>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>
    </Grid>

    那么会发生什么? Datagrid假定整个 DataTemplate 的宽度(好吧,它的底层控件设置为 DataTemplates 大小,然后 * 列假定所有空白空间。当您尝试调整包含此代码的整个窗口的大小时,它将正确增长,扩展 * 列但似乎不是缩小“注册”并保持您将其扩展为的大小,在其上应用滚动条并忘记它。

    到目前为止,我尝试的是为 ItemsControl 设置宽度,它的底层父级如 Grid 等,也将大小设置为 ContentControl , StackPanel , ScrollViewer和它的父网格。
    我也试过使用 scrollviewer直接上 Datagrid这产生了一个癫痫的“一百万调整一秒”的场景。我也玩过 HorizontalAlignments在某些情况下,我确实设法让水平滚动条正确显示,但不幸的是,这让我的 DataGrid的 * 列假设 Auto宽度而不是 Star所以 DataGrid右边开始有一个空白区域(不幸的是, Not Acceptable ......)

    我知道为了让水平滚动条工作,滚动查看器的父级或子级需要设置宽度,我想我无法确定我需要在哪里限制它。 DataGrids 需要随着主窗口无限扩展,同时让第一列填满所有可用空间。
    如果您需要有关此的更多信息,请立即告诉我,我会很乐意回答。

    最佳答案

    在我看来,这只是可怕的 StackPanel 的另一个案例。布局问题。这个问题一次又一次地出现,我承认我在开始学习 WPF 时遇到了同样的问题。 StackPanel不考虑其父级的可用大小,而其他 Panel例如 DockPanelGrid (是的,这实际上也是 Panel)。

    How to: Choose Between StackPanel and DockPanel 中有解释MSDN上的页面:

    Although you can use either DockPanel or StackPanel to stack child elements, the two controls do not always produce the same results. For example, the order that you place child elements can affect the size of child elements in a DockPanel but not in a StackPanel. This different behavior occurs because StackPanel measures in the direction of stacking at Double.PositiveInfinity; however, DockPanel measures only the available size.


    StackPanel应该只真正用于对齐多个项目,例如 Button s 或直线上的其他控件,可用空间不是问题。所以无论如何,解决方案应该很简单......只需删除 StackPanel来自 ScrollViewer .无论如何,它似乎没有任何用途。

    更新>>>

    再看一遍,好像你说的问题出在 DataTemplate , 对?您可以通过设置 ItemsControl.HorizontalContentAlignment 来解决这个问题。属性(property)给 Stretch .这将确保每个项目保持在 ItemsControl 的边界内。 .

    我也会删除你的 BindingGrid.Width因为你不需要它...一个 child Grid将占用父级的全部空间 Grid默认情况下。如果这些想法不起作用,只需简化您的问题。说真的,如果您遵循我在评论中给您的帮助中心链接页面中的建议,那么您将解决问题,或者能够回到这里并提供一个完整但简洁的示例,我们可以测试。

    关于WPF ContentControl 宽度会增加,但在包裹在 ScrollViewer 中时不会缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21782730/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com