wpf: Getting wrong size while calculate the size of contenttemplate.
- 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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 18 (9 by maintainers)
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
It is not clear to me what you are trying to achieve but this is not how templating works, and you almost definitely do not want to set the
Content
andContentTemplate
to the same object.Templates are meant to describe a visual tree. Every time an object is represented by a template, the template creates a new visual tree based on that description. Any changes you make in that visual tree (like expanding an expander here) do not reflect back to the tree description in the template. Therefore the object you are measuring (template) is not the object you see in the window (tree created by the template).
If you want to measure the instantiated tree, you need to measure the content control, not the template (i.e.
ItemTemplate.DesiredSize
has the size you are looking for, notItemTemplate.ContentTemplate.DesiredSize
).