Home > Back-end >  WPF bind to property of the ItemsPanel of an ItemsControl
WPF bind to property of the ItemsPanel of an ItemsControl

Time:04-04

I've written a code example below:

<StackPanel>
    <TextBlock>Appointments today</TextBlock>

    <ItemsControl ItemsSource="{Binding Appointments}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <controls:CalendarMonthDayEventsItemsPanel OutsideViewportCount="..." />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border>
                    <TextBlock Text="{Binding Title}" />
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    
    <StackPanel Orientation="Horizontal">
        <TextBlock> </TextBlock>
        <TextBlock Text="{Binding ......}" /> <!-- Should show number of items in ItemsControl that are outside view, using  CalendarMonthDayEventsItemsPanel.OutsideViewportCount -->
        <TextBlock>more items</TextBlock>
    </StackPanel>

</StackPanel>

I Created a custom Panel, the CalendarMonthDayEventsItemsPanel, and I use that panel as the itemspanel of the ItemsControl. In the panel's layout code, I determine how many items (panel childs) are outside the bounds of the panel. The property OutsideViewportCount contains the number of items that are outside of the visible bounds of the CalendarMonthDayEventsItemsPanel.

Now I want to show the value of OutsideViewportCount in a TextBlock that's below the ItemsControl.

This means I have to create some kind of binding, but everything I tried doesn't work. Does someone have a solution or a better approach to achieve the same result?

CodePudding user response:

Maybe you can make use of Tag property of ItemsPanel to relay the value of ItemsPanel as a workaround.

<ItemsControl x:Name="A"
              ItemsSource="{Binding Appointments}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <local:CalendarMonthDayEventsItemsPanel OutsideViewportCount="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=Tag, Mode=OneWayToSource}"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    ...
</ItemsControl>

<StackPanel Orientation="Horizontal">
    <TextBlock> </TextBlock>
    <TextBlock Text="{Binding ElementName=A, Path=Tag, Mode=OneWay}"/>
    <TextBlock>more items</TextBlock>
</StackPanel>
  • Related