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

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 网格单元没有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

[db:关键词]

本文发布于:2023-04-24 23:15:28,感谢您对本站的认可! 本文链接:https://www.elefans.com/category/jswz/34/1074386.html 版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。 本文标签:网格   单元   文本   WPF
  • 上一篇: 在JavaScript中链接逻辑运算符的短手?(short hand for chaining logical operators in javascript?)
  • 下一篇: 如何使用SASS设置Webpack(How to set Webpack with SASS)
  • win10蓝屏终止代码CRITICAL_PROCESS_DIED解决方法
  • 您可以尝试添加 --skip-broken 选项来解决该问题 您可以尝试执行:rpm -Va --nofiles --nodigest 解决方案
  • 关于无线网络波动大的解决办法
  • Windows10 关于系统中断CPU占用过高导致电脑变卡的解决办法
  • VS 2019 点击页面自动定位到解决方案资源管理器目录位置
  • (亲测解决)VMware打开需要半天才进入、打开系统很慢、运行很慢解决办法
  • Typora官网下载的最新版本mac10.13以下版本用不了的解决办法
  • 成功解决ModuleNotFoundError: No module named ‘torch._C‘
  • MySQL:由于找不到VCRUNTIME140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题
  • 关于输出超限的解决方法
  • 谷歌地图倾斜不起作用(Google Map Tilt not working) 谷歌地图倾斜不起作用(Google Map Tilt not working)
  • wireshark 找不到wifi无线网卡的解决方法 wireshark 找不到wifi无线网卡的解决方法
  • Wireshark XML文件失败的python xml解析器。(Wireshark XML files failed python xml parser. Why?) Wireshark XML文件失败的python xml解析器。(Wireshark XML files failed python xml parser. Why?)
  • Android eclipse webview弹出不显示 Android eclipse webview弹出不显示
  • 限制浏览器不显示弹出窗口“你想保存密码”吗? 限制浏览器不显示弹出窗口“你想保存密码”吗?
  • c#在字符串数据超过255个字符时插入到Excel中时出错(c# Error inserting to Excel when string data is more than 255 chars) c#在字符串数据超过255个字符时插入到Excel中时出错(c# Error inserting to Excel when string data is more than 255 chars)
  • 不显示弹出窗口在jquery对话框中单击取消 不显示弹出窗口在jquery对话框中单击取消
  • 仅在传单弹出窗口中显示填充的属性(而不显示“空"属性) 仅在传单弹出窗口中显示填充的属性(而不显示“空"属性)
  • 滚动后,ios,UITableView,缩进意外更改(ios, UITableView, indent changed unexpectedly after scrolling) 滚动后,ios,UITableView,缩进意外更改(ios, UITableView, indent changed unexpectedly after scrolling)
  • IE9中不显示Ajax Modal弹出窗口 IE9中不显示Ajax Modal弹出窗口
  •