简体   繁体   中英

TextBlock doesn't want to adjust grid columns

I'm preparing control to present some data. It is built in the following way:

-> ScrollViewer
--> StackPanel
---> Border
----> Grid
---> Border
----> Grid
...
---> Border
----> Grid

And there is my code for each item

    public class PresenterItem : Border
{
    // Variables
    private Submission submission;
    private int index;

    private Grid grid = new Grid();

    // Constructor
    public PresenterItem(int i, Submission subm)
    {
        index = i;
        submission = subm;
        Child = grid;

        Background = Global.GET_BRUSH("ItemBackground");
        CornerRadius = new CornerRadius(5);
        Margin = new Thickness(0, 0, 0, 10);
        Padding = new Thickness(5);

        grid.ShowGridLines = true;


        grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(40, GridUnitType.Pixel) });
        grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(2, GridUnitType.Star) });

        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(40, GridUnitType.Pixel) });
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30, GridUnitType.Pixel) });
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

        InsertContent();
    }

    private Label CreateLabel(int row, int column, string content, int columnSpan = 1)
    {
        Label newLabel = new Label();
        newLabel.Content = content;

        Grid.SetRow(newLabel, row);
        Grid.SetColumn(newLabel, column);
        Grid.SetColumnSpan(newLabel, columnSpan);

        grid.Children.Add(newLabel);

        return newLabel;
    }
    private TextBlock CreateTextBlock(int row, int column, int columnSpan = 1)
    {
        TextBlock newTextBlock = new TextBlock();
        newTextBlock.Foreground = Brushes.Silver;

        Grid.SetRow(newTextBlock, row);
        Grid.SetColumn(newTextBlock, column);
        Grid.SetColumnSpan(newTextBlock, columnSpan);

        grid.Children.Add(newTextBlock);

        return newTextBlock;
    }

    private void InsertContent()
    {
        // Number
        Label number = CreateLabel(0, 0, $"#{index + 1}");
        number.HorizontalAlignment = HorizontalAlignment.Center;
        number.VerticalAlignment = VerticalAlignment.Center;
        number.FontSize = 17;

        // Header
        Label header = CreateLabel(0, 1, $"{submission.Name} ({submission.Rank})");
        header.Foreground = Global.GET_BRUSH("HeaderForeground");
        header.HorizontalAlignment = HorizontalAlignment.Left;
        header.VerticalAlignment = VerticalAlignment.Center;
        header.FontSize = 17;

        // Timestamp
        TextBlock timestamp = CreateTextBlock(0, 2);
        timestamp.Inlines.Add(new Run("Timestamp"));
        timestamp.Inlines.Add(new Run($"{submission.Timestamp}") { Foreground = Global.GET_BRUSH("HeaderForeground") });

        timestamp.HorizontalAlignment = HorizontalAlignment.Right;
        timestamp.VerticalAlignment = VerticalAlignment.Center;
        timestamp.FontSize = 13.5;

        // Range
        TextBlock range = CreateTextBlock(1, 1);
        range.Inlines.Add(new Run("Some text "));
        range.Inlines.Add(new Run($"{submission.Range.ToStringWithDayNames()}") { Foreground = Global.GET_BRUSH("HeaderForeground") });

        range.HorizontalAlignment = HorizontalAlignment.Left;
        range.VerticalAlignment = VerticalAlignment.Center;
        range.Margin = new Thickness(5, 0, 0, 0);
        range.FontSize = 13.5;

        // Conflict
        Label conflict = CreateLabel(1, 2, "Nie wykryto konfliktu");
        conflict.Foreground = Global.GET_BRUSH("GreenForeground");
        conflict.HorizontalAlignment = HorizontalAlignment.Right;
        conflict.VerticalAlignment = VerticalAlignment.Center;
        conflict.FontSize = 13.5;

        // Content
        TextBlock content = CreateTextBlock(2, 1, 2);
        content.Inlines.Add(new Run($"{submission.Content}"));

        cotent.HorizontalAlignment = HorizontalAlignment.Left;
        content.VerticalAlignment = VerticalAlignment.Top;
        content.Margin = new Thickness(5, 0, 0, 0);
        content.TextWrapping = TextWrapping.WrapWithOverflow;
        content.FontSize = 13.5;
    }

How it looks like

It perfectly works but when I added last TextBlock whole control is stretching to the right. In designer the same way of creating elements works, but in code no. What am I doing wrong?

I would like to achieve this effect with column 1 and 2 with the same width everywhere. What I want

You have to set the dependency property Grid.IsSharedSizeScope to true in the StackPanel and then set the Property SharedSizeGroup for every ColumnDefinition to a string that definies the group with same size.

I dealt with this problem on my own. If someone will be having the same problem like me, I will write how to repair it.

In the place where my app initializes ScrollViewer I set property HorizontalScrollBarVisibility to Hidden . After setting this property to Disabled everything starts to work correctly.

There is something about this: https://crmdev.wordpress.com/2010/01/16/how-to-deal-with-stubborn-silverlight-aka-stubborn-me/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM