Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find UserControl width in WPF?
    text
    copied!<pre><code>&lt;UserControl x:Class="JIMS.View.Settings.Settings" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="SettingsWindow"&gt; &lt;Border Style="{StaticResource WindowBorderStyle}" Height="100"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;my:TitleBar Title="settings"/&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;StackPanel Orientation="Vertical" Margin="10,0,0,0"&gt; &lt;Label&gt;Theme :&lt;/Label&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Vertical" Width="200" Margin="0,0,10,0"&gt; &lt;ComboBox Name="cmbTheme" ItemsSource="{Binding Path=Themes}" &gt;&lt;/ComboBox&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/UserControl&gt; </code></pre> <p>This is my UserControl and i have not set its Width and Height properties. But in some codebehind i want to get the Height and width of this UserControl and i am not able to get them.</p> <pre><code>double width=uctrl.Width; </code></pre> <p>It gives me <code>NaN</code> while </p> <pre><code>double width=ctrl.ActualWidth; </code></pre> <p>giving me <code>0</code></p> <p>The code where i need width and height</p> <pre><code>private void OpenChild(UserControl ctrl) { bool alreadyExist = false; ctrl.Uid = ctrl.Name; foreach (UIElement child in JIMSCanvas.Children) { if (child.Uid == ctrl.Uid) { alreadyExist = true; Canvas.SetZIndex(child, GetMaxZIndex); } } if (!alreadyExist) { JIMSCanvas.Children.Add(ctrl); JIMSCanvas.InvalidateMeasure(); double top = (JIMSCanvas.ActualHeight - ctrl.Height) / 2; double left = (JIMSCanvas.ActualWidth - ctrl.Width) / 2; Canvas.SetLeft(ctrl, left); Canvas.SetTop(ctrl, top); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload