height


How to calculate Height of any control which is loaded in DataTemplate?


I want to get the height of the control which is loaded in DataTemplate when the width is set to 100. I have used the below code, but always returns the size 0,20. Any suggestion on this?
<DataTemplate x:Name="dataTemplate">
<Grid>
<TextBlock Text="{Binding Path=Name}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
var record = new UserInfo() { Name = "ASKL ALASO DKADOLD ADKIKAM AMDKI ADKAI AKDKI" };
var contentControl = new ContentControl();
contentControl.Measure(new Size());
contentControl.Content = record;
contentControl.ContentTemplate = App.Current.Resources["dataTemplate"] as DataTemplate;
contentControl.Measure(new Size(100, Double.PositiveInfinity));
var size = contentControl.DesiredSize;
I have used the below code, but always returns the size 0,20. Any suggestion on this?
This is because you didn't render your ContentControl on the layout, you can render it for example like this:
var record = new UserInfo() { Name = "ASKL ALASO DKADOLD ADKIKAM AMDKI ADKAI AKDKI" };
var contentControl = new ContentControl();
stackPanel.Children.Add(contentControl); //Add this ContentControl to the childeren collection of a StackPanel to show it
//contentControl.Measure(new Size());
contentControl.Content = record;
contentControl.ContentTemplate = App.Current.Resources["dataTemplate"] as DataTemplate;
contentControl.Measure(new Size(100, Double.PositiveInfinity));
var size = contentControl.DesiredSize;
Then now, you can get the size of your ContentControl, by my side it is 94,100.
I'm not sure this size is the one you actually want, from your title, you want to calculate Height of any control which is loaded in DataTemplate, but this size is the size of ContentControl, I think what you need is the height of the Grid, or TextBlock inside of this ContentControl. To get this two control, you can code like this after the ContentControl is fully rendered:
var grid = contentControl.ContentTemplateRoot as Grid;
var textblock = grid.Children.FirstOrDefault() as TextBlock;
And be aware that the DesiredSize is not the rendered size of the control, you can refer to the Remarks part of UIElement.DesiredSize property.

Related Links

Video height won't work
change line height in sublime text default theme after changing the font-size
How to have autogrow children with flexbox?
Kalman Filter for height and acceleration
Isotope last items in row with equal height
How to set the detail height programatically in ActiveReports
Looking for a way to change the fixed height of page header in Access 2007
CSS Background image not showing with height auto
Binary Tree find height of a specific Node
Re-size the Text and Button/Label in Livecode
Overlay a background image onto the web page
WooCommerce - change price based on product_id, width and height
Susy gallery breaks height for display table and table-cell
Flex Slider full height and width without stretching
mPDF set Height for SetHTMLHeader
How to Set Height of PdfPTable in iTextSharp

Categories

HOME
caching
ibm-watson-cognitive
vhdl
puzzle
paramiko
laravel-5.2
hana
apollo
quill
filter
currency
onenote-api
sign
session-timeout
pyyaml
ada
flat-file
ms-access-2007
vs2017
kadanes-algorithm
raphael
tcpclient
fabric
kong
rhandsontable
web-frontend
ggmap
fileinfo
forum
pitest
gpib
vegan
getjson
lmdb
svnkit
pari
entity-system
m2e
superpowered
precedence
openshift-enterprise
es-shell
mongoexport
agent
twiki
wcf-security
floating-accuracy
dart-pub
control-flow-graph
datalog
catia
onmouseover
nsjsonserialization
chown
tiddlywiki
maintenance
chrome-remote-desktop
tofixed
uicollectionviewlayout
date-range
vici
unsatisfiedlinkerror
dojox.charting
spatial-query
search-regex
supportmapfragment
concur
anti-cheat
flexjson
asp.net-mvc-2
spservices
jta
vips
ipojo
xaml-designer
guzzle6
hiera
virus
searchkick
django-1.6
kallithea
belongs-to
left-recursion
and-operator
ibmsbt
dmp
valence
mfmailcomposeviewcontroll
picat
magicalrecord-2.2
confusion-matrix
bigint
convex-polygon
dynamic-binding
haskell-platform
multiple-conditions
qss
socketstream
cosm
plone-funnelweb
shim
hinstance
serp
rubycas
nagle
adobe-contribute
post-redirect-get
jquery-ui-button
web-statistics
dirty-data
msf
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App