WPF grid cells in C# no text

故事扮演 提交于 2019-12-25 03:11:45

问题


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.

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.


回答1:


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());



回答2:


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.



来源:https://stackoverflow.com/questions/13415897/wpf-grid-cells-in-c-sharp-no-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!