Silverlight XAML绑定元素名称

Silverlight XAML绑定元素名称,silverlight,xaml,binding,Silverlight,Xaml,Binding,我有以下XAML: <sdk:Label Content="{Binding RefreshTextToggle, Converter={StaticResource enumToText}, ConverterParameter=ItemsOfInterest,FallbackValue='Please select items of interest to you'}" Style="{StaticResource

我有以下XAML:

            <sdk:Label Content="{Binding RefreshTextToggle, Converter={StaticResource enumToText}, ConverterParameter=ItemsOfInterest,FallbackValue='Please select items of interest to you'}" 
                       Style="{StaticResource StandardLabel}"
                       Height="{Binding ElementName=ItemsOfInterest,Path=Height}"/>

            <ListBox Name="ItemsOfInterest" 
                     ItemsSource="{Binding Path=ItemsOfInterest}" 
                     Margin="5"
                     MinHeight="25"
                     Width="250"
                     HorizontalAlignment="Left">

感兴趣的项目的高度是动态的,取决于其中有多少元素

有人看到我的高度绑定有什么问题吗?它甚至与ItemsOfInterest的大小不一样。

您应该绑定到它,它指定了它的排列高度。“高度”属性允许您设置固定高度,但在排列时不能确切告诉您其高度。
编辑:

事实上,这是一个与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>
你也可以把它包装成一个很好的附加行为。
编辑: 这就是这样一种行为:

<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.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;
        }
    }
}
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
        ///////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Identifies the <c>HeightElement</c> attached dependency property.  This field is read-only.
        /// </summary>
        /// <value>The identifier for the <c>HeightElement</c> attached dependency property.</value>
        public static readonly DependencyProperty HeightElementProperty = DependencyProperty.RegisterAttached("HeightElement",
            typeof(FrameworkElement), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null, OnHeightElementPropertyValueChanged));

        /// <summary>
        /// Gets the value of the <see cref="HeightElementProperty"/> attached property for the specified <see cref="FrameworkElement"/>.
        /// </summary>
        /// <param name="obj">The object to which the attached property is retrieved.</param>
        /// <returns>
        /// The value of the <see cref="HeightElementProperty"/> attached property for the the specified <see cref="FrameworkElement"/>.
        /// </returns>
        public static FrameworkElement GetHeightElement(FrameworkElement obj) {
            if (obj == null) throw new ArgumentNullException("obj");
            return (FrameworkElement)obj.GetValue(HeightElementProperty);
        }

        /// <summary>
        /// Sets the value of the <see cref="HeightElementProperty"/> attached property to the specified <see cref="FrameworkElement"/>.
        /// </summary>
        /// <param name="obj">The object to which the attached property is written.</param>
        /// <param name="value">
        /// The new value of the <see cref="HeightElementProperty"/> attached property to the specified <see cref="FrameworkElement"/>.
        /// </param>
        public static void SetHeightElement(FrameworkElement obj, FrameworkElement value) {
            if (obj == null) throw new ArgumentNullException("obj");
            obj.SetValue(HeightElementProperty, value);
        }

        /// <summary>
        /// Called when <see cref="HeightElementProperty"/> is changed.
        /// </summary>
        /// <param name="d">The dependency object that was changed.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        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;

        }

        ///////////////////////////////////////////////////////////////////////////////////
        // SizeChangedEventHandler
        ///////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Identifies the <c>SizeChangedEventHandler</c> attached dependency property.  This field is read-only.
        /// </summary>
        /// <value>The identifier for the <c>SizeChangedEventHandler</c> attached dependency property.</value>
        private static readonly DependencyProperty SizeChangedEventHandlerProperty = DependencyProperty.RegisterAttached("SizeChangedEventHandler",
            typeof(SizeChangedEventHandler), typeof(SizeSynchronizationBehavior), new PropertyMetadata(null));

        /// <summary>
        /// Gets the value of the <see cref="SizeChangedEventHandlerProperty"/> attached property for the specified <see cref="FrameworkElement"/>.
        /// </summary>
        /// <param name="obj">The object to which the attached property is retrieved.</param>
        /// <returns>
        /// The value of the <see cref="SizeChangedEventHandlerProperty"/> attached property for the the specified <see cref="FrameworkElement"/>.
        /// </returns>
        private static SizeChangedEventHandler GetSizeChangedEventHandler(FrameworkElement obj) {
            if (obj == null) throw new ArgumentNullException("obj");
            return (SizeChangedEventHandler)obj.GetValue(SizeChangedEventHandlerProperty);
        }

        /// <summary>
        /// Sets the value of the <see cref="SizeChangedEventHandlerProperty"/> attached property to the specified <see cref="FrameworkElement"/>.
        /// </summary>
        /// <param name="obj">The object to which the attached property is written.</param>
        /// <param name="value">
        /// The new value of the <see cref="SizeChangedEventHandlerProperty"/> attached property to the specified <see cref="FrameworkElement"/>.
        /// </param>
        private static void SetSizeChangedEventHandler(FrameworkElement obj, SizeChangedEventHandler value) {
            if (obj == null) throw new ArgumentNullException("obj");
            obj.SetValue(SizeChangedEventHandlerProperty, value);
        }

        #endregion // Dependency Properties
    }

}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
命名空间SilverlightApplication3{
公共部分类主页面:UserControl{
公共主页(){
初始化组件();
}
专用int计数器;
私有无效按钮\u单击(对象发送者,路由目标e){
这个.counter++;
this.listBox1.Items.Add(counter.ToString());
}
}
公共静态类大小同步行为{
#区域依赖属性
///////////////////////////////////////////////////////////////////////////////////
//高度元素
///////////////////////////////////////////////////////////////////////////////////
/// 
///标识HeightElement附加的依赖项属性。此字段为只读。
/// 
///HeightElement附加依赖项属性的标识符。
公共静态只读DependencyProperty HeightElementProperty=DependencyProperty.RegisterAttached(“HeightElement”,
typeof(FrameworkElement)、typeof(SizeSynchronizationBehavior)、new PropertyMetadata(null,OnHeightElementPropertyValueChanged));
/// 
///获取指定对象的附加属性的值。
/// 
///检索附加属性的对象。
/// 
///指定对象的附加属性的值。
/// 
公共静态FrameworkElement GetHeightElement(FrameworkElement obj){
如果(obj==null)抛出新的ArgumentNullException(“obj”);
返回(FrameworkElement)对象GetValue(HeightElementProperty);
}
/// 
///将附着特性的值设置为指定的值。
/// 
///向其写入附加属性的对象。
/// 
///将附加属性的新值指定给指定的。
/// 
公共静态void SetHeightElement(FrameworkElement对象,FrameworkElement值){
如果(obj==null)抛出新的ArgumentNullException(“obj”);
对象设置值(HeightElementProperty,值);
}
/// 
///更改时调用。
/// 
///已更改的依赖项对象。
///包含事件数据的实例。
私有静态无效OnHeightElementPropertyValueChanged(DependencyObject d,DependencyPropertyChangedEventArgs e){
FrameworkElement=d作为FrameworkElement;
if(元素==null)
返回;
SizeChangedEventHandler高度SizeChangedEventHandler=GetSizeChangedEventHandler(元素);
如果(heightSizeChangedEventHandler==null){
heightSizeChangedEventHandler=(发送方,事件参数)=>{
框架元素he=GetHeightElement(元素);
如果(他!=null)
元素高度=实际高度;
};
SetSizeChangedEventHandler(元件,高度尺寸ChangedEventHandler);
}
FrameworkElement heightElement=e.OldValue作为FrameworkElement;
如果(heightElement!=null)
heightElement.SizeChanged+=heightSizeChangedEventHandler;
heightElement=e.NewValue作为框架元素;
如果(heightElement!=null)
heightElement.SizeChanged+=heightSizeChangedEventHandler;
}
///////////////////////////////////////////////////////////////////////////////////
//SizeChangedVenthandler
///////////////////////////////////////////////////////////////////////////////////
/// 
///标识SizeChangedEventHandler附加的依赖项属性。此字段为只读。
/// 
///SizeChangedEventHandler附加依赖项属性的标识符。
私有静态只读DependencyProperty SizeChangedEventHandler属性=DependencyProperty.RegisterAttached(“SizeChangedEventHandler”,
typeof(SizeChangedEventHandler)、typeof(SizeSynchronizationBehavior)、新属性元数据(null);
/// 
///获取指定对象的附加属性的值。
/// 
///检索附加属性的对象。
/// 
///指定对象的附加属性的值。
/// 
私有静态SizeChangedEventHandler GetSizeChangedEventHandler(FrameworkElement obj){
如果(obj==null)抛出新的ArgumentNullException(“obj”);
返回(SizeChangedEventHandler)obj.GetValue(SizeChangedEventHandler属性);
}
/// 
///将附着特性的值设置为指定的值。
/// 
///向其写入附加属性的对象。
/// 
///将附加属性的新值指定给指定的。
/// 
专用静态无效设置SizeChangedEventHandler(FrameworkElement obj,SizeChangedEventHandle