WPF 网格列最大宽度未强制执行

发布于 2024-07-13 22:34:20 字数 1502 浏览 2 评论 0原文

这个问题源于无法让我的 TextBlock 换行。 基本上,作为最后的尝试,我在容器网格的列上设置 MaxWidth。 我惊讶地发现我的子标签和文本框仍然可以做他们想做的任何事情(坏孩子,BAD)并且不受我的网格列的 MaxWidth =“200”的限制。

我真正想做的是让我的 TextBlock 填充可用宽度并在必要时换行。 到目前为止,在宇宙中每个已知父级上尝试了 Horizo​​ntalAlignment="Stretch" 的许多变体之后,除了在 TextBlock 上设置显式 MaxWidth="400" 或任何数字之外,没有任何效果。 这不好,因为我需要 TextBlock 填充可用宽度,而不是受某个固定数字的限制。 谢谢!

<ItemsControl>
   <ItemsControl.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MaxWidth="200" SharedSizeGroup="A" />
                <ColumnDefinition MaxWidth="200" SharedSizeGroup="B" />
            </Grid.ColumnDefinitions>

            <Label VerticalAlignment="Top" Margin="0 5 0 0" Grid.Column="0" Style="{StaticResource LabelStyle}" Width="Auto" Content="{Binding Value.Summary}" />
            <TextBlock Grid.Column="1" Margin="5,8,5,8" FontWeight="Normal"
                       Background="AliceBlue"
                       Foreground="Black" Text="{Binding Value.Description}" 
                       HorizontalAlignment="Stretch"
                       TextWrapping="Wrap" Height="Auto" />
           </Grid>
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

This problem stems from not being able to get my TextBlock to wrap. Basically as a last-ditch attempt I am setting MaxWidth on my container grid's columns. I was surprised to find that my child label and textbox still do whatever they want (bad children, BAD) and are not limited by my grid column's MaxWidth="200".

What I'm really trying to do is let my TextBlock fill available width and wrap if necessary. So far after trying many variations of HorizontalAlignment="Stretch" on every known parent in the universe, nothing works, except setting an explicit MaxWidth="400" or whatever number on the TextBlock. This is not good because I need the TextBlock to fill available width, not be limited by some fixed number. Thanks!

<ItemsControl>
   <ItemsControl.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MaxWidth="200" SharedSizeGroup="A" />
                <ColumnDefinition MaxWidth="200" SharedSizeGroup="B" />
            </Grid.ColumnDefinitions>

            <Label VerticalAlignment="Top" Margin="0 5 0 0" Grid.Column="0" Style="{StaticResource LabelStyle}" Width="Auto" Content="{Binding Value.Summary}" />
            <TextBlock Grid.Column="1" Margin="5,8,5,8" FontWeight="Normal"
                       Background="AliceBlue"
                       Foreground="Black" Text="{Binding Value.Description}" 
                       HorizontalAlignment="Stretch"
                       TextWrapping="Wrap" Height="Auto" />
           </Grid>
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

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

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

发布评论

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

评论(2

迷路的信 2024-07-20 22:34:20

我尝试通过将 Grid 元素之间的所有内容粘贴到 Kaxaml 中来复制您的问题,但所有内容都会按照您的预期进行换行。 (我在您进行绑定的位置插入了常规字符串并删除了标签样式)。

问题可能出在树的更高层。

我建议将块粘贴到 Kaxaml 或类似的文件中进行测试,看看哪个父级会破坏您的 UI。

I've tried to replicate your problem by pasting everything between your Grid elements in to Kaxaml but everything wraps as you would expect it to. (I inserted regular strings where you were doing bindings and removed the Label style).

It could be that the problem is higher up the tree.

I'd suggest pasting chunks in to Kaxaml or similar to test and see which parent breaks your UI.

三月梨花 2024-07-20 22:34:20

我提供了这个问题的答案,只是它使用的是 ListView 而不是 ItemsControl,但问题可能是相同的。 您的 ItemPresenter 周围可能有一个 ScrollViewer,并且您需要编辑 ItemsControl 模板的副本。

WPF ListView TextBlock TextWrapping

I provided an answer to this question, only it was using an ListView instead of an ItemsControl but the issue is likely the same. There is probably a ScrollViewer surrounding your ItemPresenter and you need to edit a copy of the ItemsControl template.

WPF ListView TextBlock TextWrapping

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文