如何防止 Silverlight xaml Grid 中的列占据整行?

发布于 2025-01-05 18:07:24 字数 791 浏览 2 评论 0原文

我想在网格中显示两列。第一个是文本块,有时比容纳它的行长。第二个是一个按钮。有没有办法为文本块提供尽可能多的空间,同时仍然为紧随其后的按钮留出空间?

当我使用以下代码时,文本块有时会将按钮推到网格的可视区域之外。

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Text="{Binding Description}" HorizontalAlignment="Stretch" />
    <Button Margin="4,0" Height="0" Width="16" Grid.Column="1" MinWidth="20" VerticalAlignment="Center" />
</Grid>

我尝试设置第一列定义 Width="*",但是当我希望它位于文本旁边时,按钮始终位于行的最末尾。有什么建议吗?

这是 Silverlight 4 中的。

谢谢。

编辑 我希望网格随着用户更改窗口大小而调整大小,因此对网格大小设置硬限制是不好的。话虽这么说,当 TextBlock 加载以及窗口大小更改时,我可以在后面的代码中手动设置 MaxWidth。它很笨重,但很有效。

I want to display two columns in my Grid. The first is a textblock that is sometimes longer than the row meant to hold it. The second is a button. Is there a way to give the textblock as much room as possible while still leaving room for the button to go immediately after?

When I use the following code, the textblock will sometimes push the button outside the viewable area of the grid.

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Text="{Binding Description}" HorizontalAlignment="Stretch" />
    <Button Margin="4,0" Height="0" Width="16" Grid.Column="1" MinWidth="20" VerticalAlignment="Center" />
</Grid>

I've tried setting the first column definition Width="*", but then the button is always at the very end of the row, when I want it next to the text. Any suggestions?

This is in Silverlight 4.

Thanks.

EDIT
I want the grid to resize as the user changes the window size, so setting a hard limit on the grid size is no good. That being said, I was able to manually set the MaxWidth in the code behind when the TextBlock loads and when the window changes size. It's clunky, but it works.

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

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

发布评论

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

评论(1

煮茶煮酒煮时光 2025-01-12 18:07:24

以下肯定会起作用..

<Grid x:Name="LayoutRoot" Background="White" Width="250">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="textBlock" Grid.Column="0" Text="Text Box" VerticalAlignment="Top"/>
    <Button x:Name="button" Grid.Column="1" Content="Button" VerticalAlignment="Top"  Width="60"/>
</Grid>

将以下行添加到构造函数中的 xaml.cs 文件中..

public MainPage()
{
    InitializeComponent();
    textBlock.MaxWidth = LayoutRoot.Width - button.Width;
}

如果有任何问题,请告诉我。

谢谢..

Following will surely work..

<Grid x:Name="LayoutRoot" Background="White" Width="250">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="textBlock" Grid.Column="0" Text="Text Box" VerticalAlignment="Top"/>
    <Button x:Name="button" Grid.Column="1" Content="Button" VerticalAlignment="Top"  Width="60"/>
</Grid>

Add Following Line to xaml.cs file in constructor..

public MainPage()
{
    InitializeComponent();
    textBlock.MaxWidth = LayoutRoot.Width - button.Width;
}

Let me know if there is any issue with it.

Thanks..

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