如何使元素自动扩展以填充空间而无需接管空间?
我正在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
(如果有很多作业,请垂直滚动)。属性horizontalsCrollBarvisibility =“隐藏”
实际上引起了这种奇怪的行为。
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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是解决您问题的解决方案,但是如果溢出,您可以尝试包裹文本,这样:
如果这有帮助,如果没有帮助,我可以更多地研究它!
I am not sure its the solution for your problem, but you can try to wrap the text if overflows, like this:
Let me know if this helps, if not i can look more into it!
我没有看到这种行为。当我尝试您的代码段时,文本不会“接管”其他列。它被剪切(请参阅下文)
sample> sample> hOTERDWARKENG
似乎您可能想设置TextWrapping属性textblock to“ 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".
Sample with TextWrapping="Wrap"