绑定到Item ItemsControl的ActualHeight

kev*_*aub 6 wpf xaml binding itemscontrol actualheight

我有两个独立的ItemsControls并排出现.在ItemsControl小号绑定到相同的ItemsSource,但他们不同的方式显示数据.

左侧显示的每个项目很可能小于右侧的相同项目.这会导致问题,因为行不会排成一行,因此我需要左侧的项目绑定到右侧的项目.

ItemsControl        ItemsControl
|Item 1         |Item 1
|Item 2         |Item 2
|Item 3         |
|Item 4         |Item 3
Run Code Online (Sandbox Code Playgroud)

如您所见,右侧的第2项更大,因此它会抛弃对齐.因此,如果我可以将左侧的项目2绑定到右侧的项目2,ActualHeight则问题将得到解决.我怎样才能在XAML中做到这一点?

编辑:为了使事情变得更复杂,ItemsControl右边需要从右向左滚动,但两者都ItemsControls需要一起向上和向下滚动.基本上,左边的一个为右边的项目提供了各种标题.

Fre*_*lad 5

跟进乔比乔伊的回答

您不能OneWayToSource在 Xaml 中为 ReadOnly Dependency Property ActualHeight进行直接绑定,但有许多解决方法。通过回答肯特Boogaart这个问题是我的最爱。它的作用是使用一个附加行为来监听SizeChangedany的事件FrameworkElement并相应地更新两个附加属性,宽度和高度。

TextBlock例如,ActualHeight可用于推入视图模型的像高度属性

<TextBlock local:ActualSizeBehavior.ObserveActualSize="True"
           local:ActualSizeBehavior.ActualHeight="{Binding Path=Height,
                                                           Mode=OneWayToSource}"
           .../>
Run Code Online (Sandbox Code Playgroud)

同步两个 ScrollViewers
您可以使用 aDependencyPropertyDescriptor来侦听VerticalOffsetProperty属性中的更改或订阅ScrollChanged事件并调用ScrollToVerticalOffset。例子

xml

<ScrollViewer Name="scrollViewerLeft"
              ScrollChanged="scrollViewerLeft_ScrollChanged">
<ScrollViewer Name="scrollViewerRight"
              ScrollChanged="scrollViewerRight_ScrollChanged">
Run Code Online (Sandbox Code Playgroud)

事件处理程序背后的代码

private void scrollViewerLeft_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    scrollViewerRight.ScrollToVerticalOffset(scrollViewerLeft.VerticalOffset);
}
private void scrollViewerRight_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    scrollViewerLeft.ScrollToVerticalOffset(scrollViewerRight.VerticalOffset);
}
Run Code Online (Sandbox Code Playgroud)

实际尺寸行为

public static class ActualSizeBehavior
{
    public static readonly DependencyProperty ActualSizeProperty =
        DependencyProperty.RegisterAttached("ActualSize",
                                            typeof(bool),
                                            typeof(ActualSizeBehavior),
                                            new UIPropertyMetadata(false, OnActualSizeChanged));
    public static bool GetActualSize(DependencyObject obj)
    {
        return (bool)obj.GetValue(ActualSizeProperty);
    }
    public static void SetActualSize(DependencyObject obj, bool value)
    {
        obj.SetValue(ActualSizeProperty, value);
    }
    private static void OnActualSizeChanged(DependencyObject dpo,
                                            DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = dpo as FrameworkElement;
        if ((bool)e.NewValue == true)
        {
            element.SizeChanged += element_SizeChanged;
        }
        else
        {
            element.SizeChanged -= element_SizeChanged;
        }
    }

    static void element_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        FrameworkElement element = sender as FrameworkElement;
        SetActualWidth(element, element.ActualWidth);
        SetActualHeight(element, element.ActualHeight);
    }

    private static readonly DependencyProperty ActualWidthProperty =
        DependencyProperty.RegisterAttached("ActualWidth", typeof(double), typeof(ActualSizeBehavior));
    public static void SetActualWidth(DependencyObject element, double value)
    {
        element.SetValue(ActualWidthProperty, value);
    }
    public static double GetActualWidth(DependencyObject element)
    {
        return (double)element.GetValue(ActualWidthProperty);
    }

    private static readonly DependencyProperty ActualHeightProperty =
        DependencyProperty.RegisterAttached("ActualHeight", typeof(double), typeof(ActualSizeBehavior));
    public static void SetActualHeight(DependencyObject element, double value)
    {
        element.SetValue(ActualHeightProperty, value);
    }
    public static double GetActualHeight(DependencyObject element)
    {
        return (double)element.GetValue(ActualHeightProperty);
    }
}
Run Code Online (Sandbox Code Playgroud)