您应该绑定到
ActualHeight,它指定了元素排列时的高度。Height属性允许您设置固定高度,但不告诉您排列后确切的高度。
实际上,这是Silverlight中已知的错误。
您需要使用SizeChanged事件,如下所示:
<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">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Add Item" Click="Button_Click" />
<ListBox x:Name="listBox1" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" SizeChanged="listBox1_SizeChanged" />
<ListBox x:Name="listBox2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" />
</Grid>
</UserControl>
有以下代码后台:
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;
}
}
}
你可能可以将这个内容整合成一个很好的附加行为。
编辑:
以下是这样一个行为:
<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">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Add Item" Click="Button_Click" />
<ListBox x:Name="listBox1" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" />
<ListBox x:Name="listBox2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" local:SizeSynchronizationBehavior.HeightElement="{Binding ElementName=listBox1}" />
</Grid>
</UserControl>
使用代码后台:
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
public static readonly DependencyProperty HeightElementProperty = DependencyProperty.RegisterAttached("HeightElement",
typeof(FrameworkElement), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null, OnHeightElementPropertyValueChanged));
public static FrameworkElement GetHeightElement(FrameworkElement obj) {
if (obj == null) throw new ArgumentNullException("obj");
return (FrameworkElement)obj.GetValue(HeightElementProperty);
}
public static void SetHeightElement(FrameworkElement obj, FrameworkElement value) {
if (obj == null) throw new ArgumentNullException("obj");
obj.SetValue(HeightElementProperty, value);
}
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) => {
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;
}
private static readonly DependencyProperty SizeChangedEventHandlerProperty = DependencyProperty.RegisterAttached("SizeChangedEventHandler",
typeof(SizeChangedEventHandler), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null));
private static SizeChangedEventHandler GetSizeChangedEventHandler(FrameworkElement obj) {
if (obj == null) throw new ArgumentNullException("obj");
return (SizeChangedEventHandler)obj.GetValue(SizeChangedEventHandlerProperty);
}
private static void SetSizeChangedEventHandler(FrameworkElement obj, SizeChangedEventHandler value) {
if (obj == null) throw new ArgumentNullException("obj");
obj.SetValue(SizeChangedEventHandlerProperty, value);
}
#endregion // Dependency Properties
}
}