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

如何解决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>
我希望这在您的限制范围内有效,如果不能,则可以帮助您自己找到解决方案!     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?