Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should bind to <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.actualheight%28v=vs.95%29.aspx" rel="nofollow">ActualHeight</a>, which specifies the height it was arranged at. The Height property allows you to set a fixed height, but doesn't tell you exactly how tall it is when arranged. <hr/> EDIT:</p> <p>Actually, this is a <a href="http://connect.microsoft.com/VisualStudio/feedback/details/522231/silverlight-grid-stackpanel-canvas-actualheight-and-acutalwidth-is-0" rel="nofollow">known bug</a> with Silverlight.</p> <p>You would have to use the SizeChanged event like so:</p> <pre><code>&lt;UserControl x:Class="SilverlightApplication3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Content="Add Item" Click="Button_Click" /&gt; &lt;ListBox x:Name="listBox1" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" SizeChanged="listBox1_SizeChanged" /&gt; &lt;ListBox x:Name="listBox2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>With a code-behind of:</p> <pre><code>using System.Windows; using System.Windows.Controls; namespace SilverlightApplication3 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private int counter; private void Button_Click(object sender, RoutedEventArgs e) { this.counter++; this.listBox1.Items.Add(counter.ToString()); } private void listBox1_SizeChanged(object sender, SizeChangedEventArgs e) { this.listBox2.Height = this.listBox1.ActualHeight; } } } </code></pre> <p>You could probably wrap this up into a nice attached behavior also. <hr/> EDIT: Here is such a behavior:</p> <pre><code>&lt;UserControl x:Class="SilverlightApplication3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SilverlightApplication3" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Content="Add Item" Click="Button_Click" /&gt; &lt;ListBox x:Name="listBox1" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" /&gt; &lt;ListBox x:Name="listBox2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" local:SizeSynchronizationBehavior.HeightElement="{Binding ElementName=listBox1}" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>With a code-behind of:</p> <pre><code>using System; using System.Windows; using System.Windows.Controls; namespace SilverlightApplication3 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private int counter; private void Button_Click(object sender, RoutedEventArgs e) { this.counter++; this.listBox1.Items.Add(counter.ToString()); } } public static class SizeSynchronizationBehavior { #region Dependency Properties /////////////////////////////////////////////////////////////////////////////////// // HeightElement /////////////////////////////////////////////////////////////////////////////////// /// &lt;summary&gt; /// Identifies the &lt;c&gt;HeightElement&lt;/c&gt; attached dependency property. This field is read-only. /// &lt;/summary&gt; /// &lt;value&gt;The identifier for the &lt;c&gt;HeightElement&lt;/c&gt; attached dependency property.&lt;/value&gt; public static readonly DependencyProperty HeightElementProperty = DependencyProperty.RegisterAttached("HeightElement", typeof(FrameworkElement), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null, OnHeightElementPropertyValueChanged)); /// &lt;summary&gt; /// Gets the value of the &lt;see cref="HeightElementProperty"/&gt; attached property for the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;The object to which the attached property is retrieved.&lt;/param&gt; /// &lt;returns&gt; /// The value of the &lt;see cref="HeightElementProperty"/&gt; attached property for the the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/returns&gt; public static FrameworkElement GetHeightElement(FrameworkElement obj) { if (obj == null) throw new ArgumentNullException("obj"); return (FrameworkElement)obj.GetValue(HeightElementProperty); } /// &lt;summary&gt; /// Sets the value of the &lt;see cref="HeightElementProperty"/&gt; attached property to the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;The object to which the attached property is written.&lt;/param&gt; /// &lt;param name="value"&gt; /// The new value of the &lt;see cref="HeightElementProperty"/&gt; attached property to the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/param&gt; public static void SetHeightElement(FrameworkElement obj, FrameworkElement value) { if (obj == null) throw new ArgumentNullException("obj"); obj.SetValue(HeightElementProperty, value); } /// &lt;summary&gt; /// Called when &lt;see cref="HeightElementProperty"/&gt; is changed. /// &lt;/summary&gt; /// &lt;param name="d"&gt;The dependency object that was changed.&lt;/param&gt; /// &lt;param name="e"&gt;The &lt;see cref="DependencyPropertyChangedEventArgs"/&gt; instance containing the event data.&lt;/param&gt; private static void OnHeightElementPropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { FrameworkElement element = d as FrameworkElement; if (element == null) return; SizeChangedEventHandler heightSizeChangedEventHandler = GetSizeChangedEventHandler(element); if (heightSizeChangedEventHandler == null) { heightSizeChangedEventHandler = (sender, eventArgs) =&gt; { FrameworkElement he = GetHeightElement(element); if (he != null) element.Height = he.ActualHeight; }; SetSizeChangedEventHandler(element, heightSizeChangedEventHandler); } FrameworkElement heightElement = e.OldValue as FrameworkElement; if (heightElement != null) heightElement.SizeChanged += heightSizeChangedEventHandler; heightElement = e.NewValue as FrameworkElement; if (heightElement != null) heightElement.SizeChanged += heightSizeChangedEventHandler; } /////////////////////////////////////////////////////////////////////////////////// // SizeChangedEventHandler /////////////////////////////////////////////////////////////////////////////////// /// &lt;summary&gt; /// Identifies the &lt;c&gt;SizeChangedEventHandler&lt;/c&gt; attached dependency property. This field is read-only. /// &lt;/summary&gt; /// &lt;value&gt;The identifier for the &lt;c&gt;SizeChangedEventHandler&lt;/c&gt; attached dependency property.&lt;/value&gt; private static readonly DependencyProperty SizeChangedEventHandlerProperty = DependencyProperty.RegisterAttached("SizeChangedEventHandler", typeof(SizeChangedEventHandler), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null)); /// &lt;summary&gt; /// Gets the value of the &lt;see cref="SizeChangedEventHandlerProperty"/&gt; attached property for the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;The object to which the attached property is retrieved.&lt;/param&gt; /// &lt;returns&gt; /// The value of the &lt;see cref="SizeChangedEventHandlerProperty"/&gt; attached property for the the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/returns&gt; private static SizeChangedEventHandler GetSizeChangedEventHandler(FrameworkElement obj) { if (obj == null) throw new ArgumentNullException("obj"); return (SizeChangedEventHandler)obj.GetValue(SizeChangedEventHandlerProperty); } /// &lt;summary&gt; /// Sets the value of the &lt;see cref="SizeChangedEventHandlerProperty"/&gt; attached property to the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;The object to which the attached property is written.&lt;/param&gt; /// &lt;param name="value"&gt; /// The new value of the &lt;see cref="SizeChangedEventHandlerProperty"/&gt; attached property to the specified &lt;see cref="FrameworkElement"/&gt;. /// &lt;/param&gt; private static void SetSizeChangedEventHandler(FrameworkElement obj, SizeChangedEventHandler value) { if (obj == null) throw new ArgumentNullException("obj"); obj.SetValue(SizeChangedEventHandlerProperty, value); } #endregion // Dependency Properties } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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