Имяэлемента привязки Silverlight XAML

#silverlight #xaml #binding

#silverlight #xaml #привязка

Вопрос:

У меня есть следующий 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">
  

Высота интересующего элемента является динамической в зависимости от количества элементов в нем.

Кто-нибудь видит, что я делаю неправильно с привязкой высоты? Он даже близко не соответствует размеру ItemsOfInterst.

Комментарии:

1. Я думаю, вам было бы лучше просто использовать grid или stackpanel для координации соотношения размеров между этими двумя элементами.

2. Я хотел бы, чтобы я мог, они на самом деле находятся в двух разных столбцах сетки.

Ответ №1:

Вы должны выполнить привязку к 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

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

}
  

Комментарии:

1. Это не работает в моем POC. Кроме того, ActualHeight возвращает высоту экрана по вертикали в полном режиме в соответствии с MS docs.

2. @Nissan — Ты смотришь на список. Фактический размер?

3. ДА. При привязке к этому параметру высота списка автоматической высоты не изменится.

4. @Nissan — Вы правы, похоже, что это работает в WPF, но не в Silverlight. Обновлено с единственным известным обходом использования SizeChanged.

5. Выглядит великолепно. Всем хорошей работы. Приятно узнать об этих угловых примерах.

Ответ №2:

Я создал простой проект, который повторяет проблему, с которой вы столкнулись:

  1. Добавлены поле списка и кнопка
  2. Привязал кнопку к высоте списка
  3. Установите высоту поля списка на «Auto»
  4. Создана другая кнопка, которая добавляет случайное количество элементов в список

При запуске поле списка увеличивается, но связанная кнопка — нет. Сейчас я работаю над тем, чтобы определить, почему это не удается.

Обновить

Хорошо, причина, по которой это не работает, заключается в том, что Auto не возвращает никакого фактического значения, которое можно использовать для определения высоты listbox с помощью этой привязки. Я пробовал все другие подходы к решению этой проблемы, но безуспешно. Будет интересно увидеть решение этой проблемы, просто полагаясь на привязку.

Комментарии:

1. Кнопка не увеличивается, потому что ее высота будет автоматической (значение по умолчанию), как и у ListBox. Таким образом, они будут занимать столько вертикального пространства, сколько необходимо для отображения их содержимого.

2. @CodeNaked согласен. Но даже указание на другие пути в Listbox, такие как ActualHeight, ItemsPanel.VisualTree. Высота и т.д. все приводит к одному и тому же конечному результату.