如何使元素自动扩展以填充空间而无需接管空间?

发布于 2025-01-28 01:50:55 字数 1283 浏览 1 评论 0原文

我正在XAML和WPF中创建自定义日历。日历由窗口自动展开的七个< grid>列组成(例如,width =“ 1*” )。

这是现在正在发生的事情:

”“在此处输入图像描述”

一个非常宽的元素“接管”列,使其他列更小,这不是我想要的。

我希望它看起来像这样:

​我知道实现这一目标的唯一方法是设置宽度属性(,但它不会自动扩展)。

这是示例代码。它会像第一个图像一样产生输出:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Hello world this is a sample very long text!" Grid.Column="1"/>
    <TextBlock Text="Hello world !" Grid.Column="2"/>
</Grid>

如何实现此期望的输出?

编辑:我在网格周围使用scrollViewer(如果有很多作业,请垂直滚动)。属性horizo​​ntalsCrollBarvisibility =“隐藏”实际上引起了这种奇怪的行为。

I am creating a custom calendar in XAML and WPF. The calendar is made up of seven <Grid> columns that can auto expand (e.g. width="1*") with the window.

Here's what's happening right now:

enter image description here

A very wide element "takes over" the columns and makes the others smaller, which is not what I want.

I want it to look like this:

enter image description here

In this example, the columns auto-expand to fill available width (window size) but an element can't make one column larger. The only way I know to achieve this is by setting the width property (but then it wouldn't auto-expand).

Here's sample code. It would produce output like the first image:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Hello world this is a sample very long text!" Grid.Column="1"/>
    <TextBlock Text="Hello world !" Grid.Column="2"/>
</Grid>

How can I achieve this desired output?

Edit: I was using a ScrollViewer around my grid (to vertically scroll in case there were many assignments). The property HorizontalScrollBarVisibility="Hidden" actually caused this strange behavior.

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

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

发布评论

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

评论(2

天邊彩虹 2025-02-04 01:50:55

我不确定这是解决您问题的解决方案,但是如果溢出,您可以尝试包裹文本,这样:

<TextBlock Text="Some long text" TextWrapping="Wrap">

如果这有帮助,如果没有帮助,我可以更多地研究它!

I am not sure its the solution for your problem, but you can try to wrap the text if overflows, like this:

<TextBlock Text="Some long text" TextWrapping="Wrap">

Let me know if this helps, if not i can look more into it!

楠木可依 2025-02-04 01:50:55

我没有看到这种行为。当我尝试您的代码段时,文本不会“接管”其他列。它被剪切(请参阅下文)

sample> sample> hOTERDWARKENG

似乎您可能想设置TextWrapping属性textblock to“ wrap”。

<TextBlock Text="Hello world this is a sample very long text!" Grid.Column="1" TextWrapping="Wrap"/>

带有textwrapping =“ wrap”的示例

I'm not seeing that behavior. When I try your code snippet, the text doesn't "take over" the other columns. It gets clipped (see below)

Sample without TextWrapping

Seems like you might want to set the TextWrapping attribute of TextBlock to "Wrap".

<TextBlock Text="Hello world this is a sample very long text!" Grid.Column="1" TextWrapping="Wrap"/>

Sample with TextWrapping="Wrap"

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