简体   繁体   English

设置动态生成的WPF画布的背景颜色?

[英]Set Background Color of Dynamically Generated WPF Canvas?

I have a dynamically created WPF Canvas element inside a ContentControl (the same thing happens in a UserControl .) Whenever I attempt to set the background, either upon creation or later on in the program, the background color won't paint (I am however able to set the background on a Label inside of the Canvas (also added dynamically.) The creation code I have looks like: 我在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;

And is called inside of the ContentControl constructor. 并且在ContentControl构造函数内部被调用。 From that I would expect the header to contain a full row of Red with a Blue rectangle around the text, but all I get is the Blue rectangle around text with most of the row left Transparent (noticeable due to the Green background of the root Grid .) Any help on this would be appreciated as it is enormously frustrating. 据此,我希望标题包含一整行Red ,文本周围带有Blue矩形,但是我得到的只是文本周围的Blue矩形,其中大部分行都保持Transparent (由于根GridGreen背景而明显) 。)在此方面的任何帮助将不胜感激,因为它极大地令人沮丧。 I'm using version 6.2 of the .NET framework on Windows 7 if that plays into it (I have noticed some other odd behaviors, but am going for dynamic generation mostly because these ContentControl s take lots of child elements and the VS 2017 XAML parser is too broken to allow them to be named - which makes them virtually useless.) 我正在Windows 7上使用.NET框架的6.2版本(我注意到它有其他奇怪的行为,但是我打算进行动态生成主要是因为这些ContentControl带有很多子元素和VS 2017 XAML解析器太破损而无法命名(这实际上使它们毫无用处。)

The solution is to use non-zero Width for ColumnDefinition: 解决方案是对ColumnDefinition使用非零宽度:

_rootGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star); // equal to Width="*" in xaml

When 0 is used, Canvas has 0 witdh. 使用0 ,Canvas的值为witdh。 But is is possible to see blue Label because Canvas doesn't clip contents on its bounds. 但是可能会看到蓝色的Label,因为Canvas不会在其边界上剪切内容。

If you try Grid ( var _headerBlock = new Grid(); ) with zero width column, there won't be anything displayed at all. 如果您尝试使用零宽度列的Grid( var _headerBlock = new Grid(); ),则根本不会显示任何内容。

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

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