简体   繁体   English

C#中的WPF网格单元格无文本

[英]WPF grid cells in C# no text

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. 我在WPF窗口中创建了一个网格。 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. 列标题,最后一列,gridseperators,grid bot和左边缘也不可见。

我的WPF窗口的当前状态

My grid is called propertiesView. 我的网格称为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. 我尝试使用TextBox代替RichTextBox,并在类构造函数中设置所有这些东西,而不是使用单独的方法。

Well it seems like you just never set the Content dependency property on your labels, or the Document of your RichTextBoxes. 好吧,好像您从未在标签或RichTextBoxes的Document上设置Content依赖项属性。

For your labels, as you set the text parameter as the DataContext, you can just add something like 对于标签,将text参数设置为DataContext时,只需添加以下内容

headerElement.SetBinding(Label.ContentProperty, new Binding());

Turns out I needed labels with textblocks, spans and runs. 原来,我需要带有文本块,跨度和运行的标签。 Style can be added on the span (through properties like Foreground, TextDecoration or FontWeight). 可以在跨度上添加样式(通过诸如Foreground,TextDecoration或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. 文本现在可以使用了,我应该能够使gridseperators工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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