WPF grid cells in C# no text

2019-08-29 10:09发布

I need to show some data in a structured way with colored letters and rows with background colors.

I made a grid in a WPF Window. It shows the textboxes and some of the labels, but none of the text. Also the column header, last column, gridseperators, grid bot and left edges are invisible.

Current status of my WPF window

My grid is called propertiesView.

Code for adding header elements (labels)

    private void AddHeaderElement(string text, int row, int col)
    {
        Label headerElement = new Label();
        headerElement.Height = cellHeight;
        headerElement.Width = cellWidth;
        headerElement.DataContext = text;
        headerElement.Background = headerBackground;
        headerElement.BorderBrush = new SolidColorBrush(Color.FromRgb(120, 120, 120));
        headerElement.BorderThickness = new Thickness(3);

        propertiesView.Children.Add(headerElement);
        Grid.SetRow(headerElement, row);
        Grid.SetColumn(headerElement, col);
    }

Code for adding cells

RichTextBox cell = new RichTextBox();

cell.Height = cellHeight;
cell.Width = cellWidth;
cell.ToolTip = toolTip;
cell.DataContext = text;
cell.Background = rowDifferent;

propertiesView.Children.Add(cell);

//box.SetValue(Grid.RowProperty, rowCount);
//box.SetValue(Grid.ColumnProperty, columnCount);
Grid.SetRow(cell, rowCount);
Grid.SetColumn(cell, columnCount);

Code for adding grid seperators

GridSplitter colSeperator = new GridSplitter();
colSeperator.Margin = new Thickness(-2.5, 0, 0, 0);
colSeperator.Width = 5;
colSeperator.ResizeDirection = GridResizeDirection.Columns;
colSeperator.ResizeBehavior = GridResizeBehavior.CurrentAndNext;
colSeperator.VerticalAlignment = VerticalAlignment.Stretch;
colSeperator.HorizontalAlignment = HorizontalAlignment.Left;

propertiesView.Children.Add(colSeperator);
Grid.SetColumn(colSeperator, 0);
Grid.SetRowSpan(colSeperator, totalRows + 1);

The tooltips do show the right text. I tried using TextBox instead of RichTextBox and setting all this stuff in the class constructor instead of a seperate method.

2条回答
爷、活的狠高调
2楼-- · 2019-08-29 10:55

Turns out I needed labels with textblocks, spans and runs. Style can be added on the span (through properties like Foreground, TextDecoration or FontWeight). Add the text to the span inside a Run, then add all spans to a textblock, which is shown through a label.

Span span = new Span();
span.Foreground = Brushes.Black;
span.Inlines.Add(new Run("Text"));

textBlock.Inlines.Add(span);

Label cell = new Label();

cell.MinHeight = cellHeight;
cell.MaxWidth = cellWidth * 3;
cell.MinWidth = cellWidth;
cell.ToolTip = "toolTip";
cell.BorderThickness = new Thickness(2);

TextBlock cellText = new TextBlock();
cellText.HorizontalAlignment = HorizontalAlignment.Stretch;
cellText.TextWrapping = TextWrapping.WrapWithOverflow;

cell.Content = cellText;

The text works now, I should be able to get the gridseperators working.

查看更多
疯言疯语
3楼-- · 2019-08-29 11:00

Well it seems like you just never set the Content dependency property on your labels, or the Document of your RichTextBoxes.

For your labels, as you set the text parameter as the DataContext, you can just add something like

headerElement.SetBinding(Label.ContentProperty, new Binding());
查看更多
登录 后发表回答