0

色付きの文字と背景色の行を使用して、構造化された方法でいくつかのデータを表示する必要があります。

WPFウィンドウでグリッドを作成しました。テキストボックスと一部のラベルが表示されますが、テキストは表示されません。また、列ヘッダー、最後の列、グリッドセパレーター、グリッドボット、および左端は表示されません。

WPFウィンドウの現在のステータス

私のグリッドはpropertiesViewと呼ばれます。

ヘッダー要素(ラベル)を追加するためのコード

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

ツールチップには正しいテキストが表示されます。RichTextBoxの代わりにTextBoxを使用し、個別のメソッドの代わりにクラスコンストラクターでこれらすべてを設定してみました。

4

2 に答える 2

1

ラベルやRichTextBoxのContent依存関係プロパティを設定したことがないようです。Document

ラベルの場合、textパラメータをDataContextとして設定すると、次のようなものを追加できます。

headerElement.SetBinding(Label.ContentProperty, new Binding());
于 2012-11-16T11:50:53.803 に答える
0

テキストブロック、スパン、および実行を含むラベルが必要であることがわかりました。スパンにスタイルを追加できます(Foreground、TextDecoration、FontWeightなどのプロパティを使用)。ラン内のスパンにテキストを追加してから、ラベルで表示されるテキストブロックにすべてのスパンを追加します。

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;

テキストが機能するようになりました。グリッドセパレーターを機能させることができるはずです。

于 2012-11-20T11:12:05.000 に答える