设置动态生成的 WPF 的背景颜色 Canvas?
Set Background Color of Dynamically Generated WPF Canvas?
我在 ContentControl
中有一个动态创建的 WPF
Canvas
元素(同样的事情发生在 UserControl
中。)每当我尝试设置背景时,无论是在创建时还是稍后在程序中,背景颜色都不会绘制(但是我能够在 Canvas
内部的 Label
上设置背景(也动态添加。)创建代码我看起来像:
_rootGrid = new Grid();
_rootGrid.Name = "_rootGrid";
_rootGrid.Margin = new Thickness(0);
_rootGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
_rootGrid.VerticalAlignment = VerticalAlignment.Stretch;
_rootGrid.RowDefinitions.Add(new RowDefinition());
_rootGrid.RowDefinitions.Add(new RowDefinition());
_rootGrid.ColumnDefinitions.Add(new ColumnDefinition());
_rootGrid.RowDefinitions[0].Height = new GridLength(24);
_rootGrid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Star);
_rootGrid.ColumnDefinitions[0].Width = new GridLength(0, GridUnitType.Star);
_headerBlock = new Canvas();
_headerBlock.Name = "_headerBlock";
_headerBlock.SetValue(Grid.RowProperty, 0);
_headerBlock.SetValue(Grid.ColumnProperty, 0);
_headerBlock.PreviewMouseLeftButtonUp += _headerBlock_PreviewMouseLeftButtonUp;
_headerBlock.Background = Brushes.Red;
_title = new Label();
_title.Name = "_title";
_title.Content = "Title";
_title.VerticalAlignment = VerticalAlignment.Center;
_title.HorizontalAlignment = HorizontalAlignment.Left;
_title.FontWeight = FontWeights.Bold;
_title.Background = Brushes.Blue;
_clientBlock = new ScrollViewer();
_clientBlock.Name = "_clientBlock";
_clientBlock.Margin = new Thickness(0);
_clientBlock.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
_clientBlock.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
_clientBlock.SetValue(Grid.RowProperty, 1);
_clientBlock.SetValue(Grid.ColumnProperty, 0);
_clientArea = new Grid();
_clientArea.Name = "_clientArea";
_clientArea.HorizontalAlignment = HorizontalAlignment.Stretch;
_clientArea.VerticalAlignment = VerticalAlignment.Stretch;
_headerBlock.Children.Add(_title);
_rootGrid.Children.Add(_headerBlock);
_clientBlock.Content = _clientArea;
_rootGrid.Children.Add(_clientBlock);
base.Content = _rootGrid;
并在 ContentControl
构造函数内部调用。由此我希望 header 包含一整行 Red
并在文本周围有一个 Blue
矩形,但我得到的只是文本周围的 Blue
矩形最左边的行 Transparent
(由于根 Grid
的 Green
背景而很明显)。任何对此的帮助将不胜感激,因为它非常令人沮丧。我在 Windows 7
上使用 .NET
框架的 6.2
版本,如果它起作用的话(我注意到一些其他奇怪的行为,但我主要是因为这些 ContentControl
s 需要很多 child 元素,而 VS 2017
XAML
解析器太坏了,不能给它们命名——这使得它们几乎毫无用处。)
解决方案是为 ColumnDefinition 使用非零宽度:
_rootGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star); // equal to Width="*" in xaml
当使用0
时,Canvas的withdh为0。但是可能会看到蓝色标签,因为 Canvas 不会在其边界上剪切内容。
如果您尝试使用零宽度列的网格 (var _headerBlock = new Grid();
),则根本不会显示任何内容。
我在 ContentControl
中有一个动态创建的 WPF
Canvas
元素(同样的事情发生在 UserControl
中。)每当我尝试设置背景时,无论是在创建时还是稍后在程序中,背景颜色都不会绘制(但是我能够在 Canvas
内部的 Label
上设置背景(也动态添加。)创建代码我看起来像:
_rootGrid = new Grid();
_rootGrid.Name = "_rootGrid";
_rootGrid.Margin = new Thickness(0);
_rootGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
_rootGrid.VerticalAlignment = VerticalAlignment.Stretch;
_rootGrid.RowDefinitions.Add(new RowDefinition());
_rootGrid.RowDefinitions.Add(new RowDefinition());
_rootGrid.ColumnDefinitions.Add(new ColumnDefinition());
_rootGrid.RowDefinitions[0].Height = new GridLength(24);
_rootGrid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Star);
_rootGrid.ColumnDefinitions[0].Width = new GridLength(0, GridUnitType.Star);
_headerBlock = new Canvas();
_headerBlock.Name = "_headerBlock";
_headerBlock.SetValue(Grid.RowProperty, 0);
_headerBlock.SetValue(Grid.ColumnProperty, 0);
_headerBlock.PreviewMouseLeftButtonUp += _headerBlock_PreviewMouseLeftButtonUp;
_headerBlock.Background = Brushes.Red;
_title = new Label();
_title.Name = "_title";
_title.Content = "Title";
_title.VerticalAlignment = VerticalAlignment.Center;
_title.HorizontalAlignment = HorizontalAlignment.Left;
_title.FontWeight = FontWeights.Bold;
_title.Background = Brushes.Blue;
_clientBlock = new ScrollViewer();
_clientBlock.Name = "_clientBlock";
_clientBlock.Margin = new Thickness(0);
_clientBlock.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
_clientBlock.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
_clientBlock.SetValue(Grid.RowProperty, 1);
_clientBlock.SetValue(Grid.ColumnProperty, 0);
_clientArea = new Grid();
_clientArea.Name = "_clientArea";
_clientArea.HorizontalAlignment = HorizontalAlignment.Stretch;
_clientArea.VerticalAlignment = VerticalAlignment.Stretch;
_headerBlock.Children.Add(_title);
_rootGrid.Children.Add(_headerBlock);
_clientBlock.Content = _clientArea;
_rootGrid.Children.Add(_clientBlock);
base.Content = _rootGrid;
并在 ContentControl
构造函数内部调用。由此我希望 header 包含一整行 Red
并在文本周围有一个 Blue
矩形,但我得到的只是文本周围的 Blue
矩形最左边的行 Transparent
(由于根 Grid
的 Green
背景而很明显)。任何对此的帮助将不胜感激,因为它非常令人沮丧。我在 Windows 7
上使用 .NET
框架的 6.2
版本,如果它起作用的话(我注意到一些其他奇怪的行为,但我主要是因为这些 ContentControl
s 需要很多 child 元素,而 VS 2017
XAML
解析器太坏了,不能给它们命名——这使得它们几乎毫无用处。)
解决方案是为 ColumnDefinition 使用非零宽度:
_rootGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star); // equal to Width="*" in xaml
当使用0
时,Canvas的withdh为0。但是可能会看到蓝色标签,因为 Canvas 不会在其边界上剪切内容。
如果您尝试使用零宽度列的网格 (var _headerBlock = new Grid();
),则根本不会显示任何内容。