C# 中的 WPF 网格单元没有文本 [英] WPF grid cells in C# no text

查看:32
本文介绍了C# 中的 WPF 网格单元没有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以结构化的方式显示一些数据,其中包含带有背景颜色的彩色字母和行.

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

我在 WPF 窗口中制作了一个网格.它显示了文本框和一些标签,但没有显示任何文本.列标题、最后一列、网格分隔符、网格机器人和左边缘也不可见.

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.

我的网格叫做 propertiesView.

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

添加单元格的代码

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

添加网格分隔符的代码

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

工具提示确实显示了正确的文本.我尝试使用 TextBox 而不是 RichTextBox 并在类构造函数中设置所有这些东西,而不是单独的方法.

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.

推荐答案

原来我需要带有文本块、跨度和运行的标签.可以在跨度上添加样式(通过 Foreground、TextDecoration 或 FontWeight 等属性).将文本添加到 Run 内的跨度,然后将所有跨度添加到通过标签显示的文本块.

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.

这篇关于C# 中的 WPF 网格单元没有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆