Bind ItemControl to actual item weight

I have two separate ones ItemsControlthat appear side by side. ItemsControlattached to the same ItemsSource, but they display data in different ways.

Each item displayed on the left is likely to be smaller than the same item on the right. This causes a problem because the lines do not line up, so I need the element on the left to snap to the element on the right.

ItemsControl        ItemsControl
|Item 1         |Item 1
|Item 2         |Item 2
|Item 3         |
|Item 4         |Item 3

As you can see, point 2 on the right is larger, so it discards the alignment. Therefore, if I can attach the left item 2 to the right part 2 ActualHeight, the problem will be solved. How to do it in XAML?

Edit: To make something more complex, ItemsControlyou need to scroll right to left to right, but both ItemsControlsneed to scroll up and down together. Basically, the left contains the sort header for the items on the right.

+5
source share
3 answers

In response to Joby Joy's answer

OneWayToSource Xaml RealOhly Dependency ActualHeight, . Kent Boogaart . , , Attached Behavior, SizeChanged FrameworkElement , .

TextBlock, , ActualHeight Height ViewModel,

<TextBlock local:ActualSizeBehavior.ObserveActualSize="True"
           local:ActualSizeBehavior.ActualHeight="{Binding Path=Height,
                                                           Mode=OneWayToSource}"
           .../>

ScrollViewers
DependencyPropertyDescriptor VerticalOffsetProperty ScrollChanged ScrollToVerticalOffset.

Xaml

<ScrollViewer Name="scrollViewerLeft"
              ScrollChanged="scrollViewerLeft_ScrollChanged">
<ScrollViewer Name="scrollViewerRight"
              ScrollChanged="scrollViewerRight_ScrollChanged">

private void scrollViewerLeft_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    scrollViewerRight.ScrollToVerticalOffset(scrollViewerLeft.VerticalOffset);
}
private void scrollViewerRight_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    scrollViewerLeft.ScrollToVerticalOffset(scrollViewerRight.VerticalOffset);
}

ActualSizeBehavior

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);
    }
}
+3

ItemsSource , ItemsControl , ( ) DataTemplate, . , ItemsControl, .

- Height ViewModel (, View VM). TwoWay ActualHeight itemsContainerStyle left-itemsControl. right-itemscontrel Height ItemsContainerStyle {One Way}. , .

, " ":   ListView , GridViewColumn.CellTemplate DataTemplates. . .

.

0

: http://www.codeproject.com/KB/WPF/BindingHub.aspx

, BindingHub:

<bindings:BindingHub 
       Visibility="Hidden"
       Socket1="{Binding ActualWidth, ElementName=Item, Mode=OneWay}"
       Socket2="{Binding ItemWidth, Mode=OneWayToSource}"
       Connect="(1 in, 2 out)"/>
0

All Articles