将 ContentPresenter 的宽度限制为其动态内容

发布于 2024-11-06 22:38:53 字数 895 浏览 3 评论 0原文

我希望能够动态调整内容控件的大小。

这是一个简单的示例:

...
<Slider x:Name="width" Minimum="40" Value="100" Maximum="300"/>
...
<ContentPresenter Width="{Binding Value, ElementName=width}" Content="Some value">
   <ContentPresenter.ContentTemplate>
      <DataTemplate>
         <Grid MaxWidth="200" MinWidth="80">
            <Rectangle Fill="Wheat" />
            <TextBlock Text="{Binding}"/>
         </Grid>
      </DataTemplate>
   </ContentPresenter.ContentTemplate>
</ContentPresenter>

在这个示例中,我想强制内容 ContentPresenter 的宽度保持在其生成的子项的最小和最大宽度之内(在本例中为 80 - 200)。

显然,通过像这样的简单示例,我可以更改滑块的范围,但我的实际情况要复杂得多。我试图将弹出屏幕的大小限制为其生成的内容。我无法在弹出窗口上设置明确的范围,因为我事先不知道内容会是什么样子。内容必须能够自我限制。

不幸的是,孩子的 MaxWidth 和 MinWidth 几乎被忽略了。当父级设置较小时,MinWidth 会导致裁剪。 MaxWidth 会产生大量空白空间。看来我必须在动态更新宽度值的同一位置设置 MaxWidth 和 MinWidth。

I would like to be able to dynamically adjust the size of a content control.

Here's a simple example:

...
<Slider x:Name="width" Minimum="40" Value="100" Maximum="300"/>
...
<ContentPresenter Width="{Binding Value, ElementName=width}" Content="Some value">
   <ContentPresenter.ContentTemplate>
      <DataTemplate>
         <Grid MaxWidth="200" MinWidth="80">
            <Rectangle Fill="Wheat" />
            <TextBlock Text="{Binding}"/>
         </Grid>
      </DataTemplate>
   </ContentPresenter.ContentTemplate>
</ContentPresenter>

Given this example, I would like to force the width of the content ContentPresenter to stay within the min and max width of its generated child (80 - 200 in this case).

Obviously with a simple example like this I could just change the range of the slider, but my real scenario is more complicated. I'm trying to restrict the size of a popup screen to its generated content. I can't set an explicit range on the popup because I have no idea what the content is going to be like before hand. The content has to be able to restrict itself.

Unfortunately MaxWidth and MinWidth on children are pretty much ignored. MinWidth results in cropping when the parent is set smaller. MaxWidth results in lots of empty space. It looks like I will have to set MaxWidth and MinWidth in the same place that I am dynamically updating the Width value.

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

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

发布评论

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

评论(1

冰火雁神 2024-11-13 22:38:53

我的建议是用自定义面板替换 ContentPresenter(或者,如果无法替换,则将自定义面板包含在 ContentPresenter 中),以您想要的方式分配您想要的确切大小。声明一个派生自 Panel 的类,然后重写 MeasureOverride 和 ArrangeOverride 是相当容易和简单的。在这种情况下,您可以使用基本 MeasureOverride 测量所有内容,然后将所需的大小指定为可用空间。在 ArrangeOverride 中,只需指定面板的直接子级,作为面板的最终大小(矩形 = 0,0,宽度,高度)。

My suggestion would be to replace the ContentPresenter with a custom panel (alternately, enclose a custom panel in the ContentPresenter if you cannot replace it) that doles out the exact size you want in the way that you want. It's fairly easy and simple to declare a class that derives from Panel and then override MeasureOverride and ArrangeOverride. In this case you could measure everything with the base MeasureOverride and then just assign the size you want as your available space. In ArrangeOverride just assign the direct child of your panel, to be the final size of the panel (rect = 0,0,width,height).

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