question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 :

ezgif com-gif-maker (10)

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:

  1. Run the attached sample
  2. Expand the content template 1
  3. Click the button “Template Height of ContentTemplate 1”
  4. you will get a size of 31, 29.
  5. Click the button “Template Height of ContentTemplate 2”
  6. you will get a size of 402,429

Please find the sample from the attachment. WpfApp2.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
miloushcommented, Aug 5, 2022

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.

1reaction
miloushcommented, Jul 5, 2022

We need to display the collection of controls.

That is not a reason to set an element as a both Content and ContentTemplate. 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?

The Size of the ItemTemplate will be (402,429). Even if visibility gets Collapsed then the size of ItemTemplate still remains as same. […] We need to get the exact item template size-based visibility changed at runtime.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to calculate Height of any control which is loaded in ...
ContentTemplate = App.Current. ... Measure(new Size(100, Double. ... Then now, you can get the size of your ContentControl , by my side it ......
Read more >
Size issue with WinUIDialogWindow
Hello Support, Could you tell me please how can i calculate size for WinUIDialogWindow . because right now i am facing problem that...
Read more >
How to: Show specific Views as RadFluidContentControl. ...
This is my contentTemplate for RadTileView (Width="Auto" and Height="Auto" didn't help with getting the tile's size match that of the actual ...
Read more >
How to support auto height in the LookUpEdit with a ...
i'm setting height to auto for the popup lookupedit, but it doesn't seem ... x:Name="objectTypeCB" AutoPopulateColumns="False" Height="Auto" ...
Read more >
[SOLVED] Create template on ui get wrong size
add this script on the root, it will create a template on the 2d screen named “ui”, but this get a very big...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found