I have a scrollable ItemsControl element on my Page:

<ItemsControl ItemsSource="{Binding MyItems, Mode=OneWay}">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ScrollViewer Focusable="False"
                          CanContentScroll="True"
                          PanningMode="VerticalOnly"
                          VerticalScrollBarVisibility="Auto">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type decorators:ItemDecoratorViewModel}">
            <TextBlock FontSize="16"
                       Text="{Binding Name}"
                       VerticalAlignment="Center"
                       TextWrapping="Wrap" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Decorator ViewModel (ItemDecoratorViewModel) has only one property:

private string _name;
public string Name
{
    get => _name;
    set => SetProperty(ref _name, value);
}

On the ViewModel of the Page (that is linked to it through DataContext property) I want to get a screen position (coords of top-left corner) of ItemTemplate (in my case it will be screen position of TextBlock) that is presenting certain ItemDecoratorViewModel inside MyItems collection.

If this certain TextBlock is currently not visible (for example, when the list of items was scrolled down and item is somewhere in the beginning of collection), I should receive a Point(0, 0).

Later on, I want to position another element (outside of ItemsControl, for example, Popup) according to these coords (or make it invisible if coords are Point(0, 0)).

Adding a Popup to every TextBlock, grouping them inside StackPanel, and controlling visibility of each Popup individually (to ensure only certain Popup is visible or none at all) doesn't work well - it makes Page load very slow if collection contains hundreds of items (with 1000 items inside MyItems collection loading time is 5 seconds instead of 1 due to Visibility and IsOpen properties of a Popup being controlled by several bindings and converters).

To solve this problem, I'm thinking about adding a single Popup somewhere outside of ItemsControl, and then changing it position/visibility in an event handler (for example, MouseDown).

Is this even possible?

0

There are 0 best solutions below