Silverlight Grid-不适合其单元格的显示值

问题描述

| 我知道我们可以使用ColumnSpan和RowSpan将项目向右或向下拉伸 但是,我有一个要在网格单元中显示的项目,并使其根据该单元中心居中,因此在文本情况下它会同时左右分布,这里我提供一张图像,其外观为“ 12345” “文字无法容纳该列,并显示为\” 234 \“,有没有办法使1和5可见,而所有列都保持不变? 文本可以更改为任何内容,并且必须正确居中,我可以破解一种显示12345的方式,但是需要一种可以与任何文本一起使用的解决方案,以防万一,并且textBlock grid.Column必须保持不变1 (对于我的情况,精确到2),对于此图片情况,不是0。     

解决方法

        解决方案4 因此,如果我现在正确理解的话,您真正想要的是一种动态居中显示文本的方法。您不想自己摆弄填充物,而是想要一个适用于所有内容的数字?我当然希望您对此表示赞赏! :)
/// <summary>
/// Takes a FrameworkElement that is spanned across
/// multiple columns and RETURNS a Thickness that
/// can be applied to the Padding property to
/// centre it across the spanned columns
/// </summary>
/// <param name=\"fe\">The FrameworkElement to be centred</param>
/// <param name=\"centerColumn\">The index of the desired centre column</param>
/// <returns>A Thickness that will center the FrameworkElement</returns>
private Thickness GetCenteringPadding(FrameworkElement fe,int centerColumn)
{
    Grid parentGrid = fe.Parent as Grid;
    if (parentGrid == null)
        throw new ArgumentException();

    // Variables
    int firstColumn = (int)fe.GetValue(Grid.ColumnProperty);
    int columnSpan = (int)fe.GetValue(Grid.ColumnSpanProperty);
    double totalWidth = 0.0;
    double leftWidth = 0.0;
    double leftPaddingWidth = 0.0;

    // Total the width for all the spanned columns
    for (int i = firstColumn; i < columnSpan + firstColumn; i++)
    {
        // This part can be dangerous,especially if you\'re using a \'*\'
        totalWidth += parentGrid.ColumnDefinitions[i].ActualWidth;
    }

    // Get the total width from the left side of the first
    //  spanned column,to the center of the \'centerColumn\'
    for (int j = firstColumn; j <= centerColumn; j++)
    {
        if (j != centerColumn)
            leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth;
        else // Only take half the width for the center column
            leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth / 2;
    }

    // Calculate the padding width
    // (Abbr. rightWidth = tW - lW,lPW = lW - rW)
    leftPaddingWidth = 2*leftWidth - totalWidth;

    // Check whether the padding needs to be on the left or the right
    if (leftPaddingWidth > 0.0)
    {
        // The excess space is on the left
        return new Thickness(leftPaddingWidth,0.0,0.0);
    }
    else
    {
        // The excess space is on the right
        return new Thickness(0.0,-leftPaddingWidth,0.0);
    }
}
几个注意事项。它使用网格中列的“ 1”。您当然可以将其更改为
.Width
,但是当您使用
ColumnDefinition Width=\"30*\"
时可能会遇到问题,因为我敢肯定您不会得到两倍的结果。其次,由于以下两个原因,此函数不会在传递的对象上设置填充:   -您可以将返回的
Thickness
添加到已应用于
Control
.Padding
中,以免破坏其格式。   -您可以传递任何
FrameworkElement
,而只有
Controls
具有填充。也许您可以将返回的厚度添加到
.Margin
属性中,或者甚至创建一个容器并在其中应用填充? 最后,您可以轻松地将两个two10ѭ合并到函数中,但是我将它们与清晰分开。 干杯。     ,        我不确定我是否完全了解您的问题的限制,因此这些解决方案都可能无法使用,但是这里有两个快速建议。 (个人而言,我更喜欢解决方案2。)
<Edit: Additional Thought>
解决方案0 如果您能够更改文本块的“ 12”,则可以在不使用容器的情况下执行解决方案2。只需将文本块跨过几列,然后调整文本块的填充以使内容居中即可!
</Edit>
解决方案1 首先,如果您当前的XAML结构如下所示:
<Grid>
    <TextBlock Grid.Column=\"2\" Text=\"12345\" />
</Grid>
考虑将其更改为类似的内容?
<Grid>
    <!-- Draw up columns so that the textblock looks as you wish -->
    <TextBlock Grid.Column=\"2\" Text=\"12345\" />

    <!-- Span an inner grid across all the columns/rows in the outer grid -->
    <Grid Grid.ColumnSpan=\"..\" 
          Grid.RowSpan=\"..\">
        <!-- All the other stuff in the Grid -->
    </Grid>
</Grid>
解决方案2 如果那行不通,请考虑将文本块放在另一个容器中,例如
Label
,并将其跨列。
<Grid>
    <!-- Use the left/right padding on the label to center the textbox -->
    <sdk:Label Grid.Column=\"0\"
               Grid.ColumnSpan=\"3\"
               Grid.Row=\"1\"
               HorizontalAlignment=\"Center\"
               Padding=\"13,0\">
        <!-- In my example,setting the left pading to 13 centered the textbox -->
        <TextBlock HorizontalAlignment=\"Center\"
                   Text=\"......!......\" />
        <!-- Text like the test above,really helped find the correct padding -->
    </sdk:Label>
</Grid>
我希望这在您的限制范围内有效,如果不能,则可以帮助您自己找到解决方案!     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...