Getting wrong size while calculate the size of contenttemplate.
See original GitHub issue- Windows version: 10
- Does the bug reproduce also in WPF for .NET Framework 4.8?: yes
- Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)? No
Problem description:
What I’m doing I have a contentcontrol in my window. I have loaded the content template to the contentcontrol. I have loaded the Expander control and ListView whose visibility is set to collapsed to the contenttemplate initially. In runtime when i click the expand button in the ExpanderControl, I will set the ListView visibility to visible.
Problem I’m trying to calculate the size of the contenttemplate using following code. But I’m getting wrong size.
this.control.Content = ItemTemplate1.ContentTemplate;
this.control.ContentTemplate = ItemTemplate1.ContentTemplate;
this.control.Measure(new Size());
var controlsize = Size.Empty;
this.control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
controlsize = this.control.DesiredSize;
Mean while if I set the listview visibility as visible in compile time itself, then I’m getting the correct size with same code shown above.
Gif :
Actual behavior:
While calculating size of the content template whose visibility is set in runtime, we are getting wrong size as shown in the gif.
Expected behavior:
Need to get the correct size of the content template.
Minimal repro:
- Run the attached sample
- Expand the content template 1
- Click the button “Template Height of ContentTemplate 1”
- you will get a size of 31, 29.
- Click the button “Template Height of ContentTemplate 2”
- you will get a size of 402,429
Please find the sample from the attachment. WpfApp2.zip
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:18 (9 by maintainers)
Top GitHub Comments
As I said before, template is a set of instructions to create elements. It does not have a size. Only the created elements have size, which is ItemTemplate or ItemTemplate1 in your application.
It is still not clear what you are trying to do or what for you need the size. You said you need to show a collection of controls, but there is no collection of controls in your sample.
That is not a reason to set an element as a both
Content
andContentTemplate
. You are asking to render a template using itself. To show a list of controls you can just add controls directly to ListView and they will be rendered as Controls.Can you describe the application? Why do you need to display a collection of controls, what do they do? Can you provide a sample project that does what you actually want to do but just has wrong sizes?
DesiredSize
returns (0,0) when the visibility is set to collapsed.https://github.com/dotnet/wpf/blob/b63c69eaf5b58e758765a37bb18064bbc94832ad/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L179-L188