简体   繁体   English

如何将单个控件实例添加到ItemsPanelTemplate?

[英]How can I add a single control instance to an ItemsPanelTemplate?

In my project, I have an ItemsControl which has as its ItemsPanel template a Grid . 在我的项目中,我有一个ItemsControl ,其中有一个Grid作为其ItemsPanel模板。 I would like to add a single control with this grid, completely independent of the ItemsControl 'Items' , which can be accessed in the code-behind. 我想为此网格添加一个控件,该控件完全独立于ItemsControl'Items' ,可以在后面的代码中对其进行访问。 What is the best way to do this? 做这个的最好方式是什么?

More detail: 更多详情:

I am implementing a keyframe animation timeline in WPF. 我正在WPF中实现关键帧动画时间轴。 I do this by having a set of 'Indicators' which is bound to an ItemsControl; 我通过绑定到ItemsControl的一组“指标”来完成此操作; the ItemsControl then positions each of these on a Grid according to their position using the Margin property: 然后,ItemsControl使用Margin属性根据它们的位置将它们中的每个放置在Grid上:

    <DataTemplate x:Key="keyIndicatorTemplate">
        <Border Width="1" Height="1" 
                BorderBrush="Black" Background="Black" BorderThickness="0" 
                HorizontalAlignment="Left" VerticalAlignment="Top"
                Margin="{Binding Converter={StaticResource ResourceKey=keyIndicatorMarginConv}}"
                ></Border>
    </DataTemplate>

This Grid of indicators then automatically sizes to the key frames, and is zoomed and panned using the ItemsControl LayoutTransform property, and the encompassing ScrollViewer: 然后,此指示器网格会自动调整为关键帧的大小,并使用ItemsControl LayoutTransform属性和包含的ScrollViewer进行缩放和平移:

<Grid>
 <ScrollViewer Name="timelineScrollViewer" Background="LightCyan" HorizontalScrollBarVisibility="Visible">

    <ItemsControl Name="KeyGridPresenter" ItemTemplate="{StaticResource ResourceKey=keyIndicatorTemplate}" >

            <ItemsControl.LayoutTransform>
                <ScaleTransform 
                    ScaleX="{Binding Path=ZoomX, ElementName=TimelineUserControl}" 
                    ScaleY="{Binding Path=ZoomY, ElementName=TimelineUserControl}">
                </ScaleTransform>
            </ItemsControl.LayoutTransform>

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid Name="KeyGrid">
                        </Grid>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

     </ItemsControl>

 </ScrollViewer>
</Grid>

I would like to add a semi-transparent 'highlighter/caret' which the user controls with the mouse, but since this design is based on the grid being an abstract 'surface' which the ItemsControl and ScrollViewer provide a 'window' to, I need to add the control in such a way that the transforms of the containers apply to it (ie a sibling or child of the grid) 我想添加一个半透明的“突出显示/插入符号”,用户可以用鼠标控制它,但是由于这种设计基于网格是抽象的“表面”, ItemsControlScrollViewer为该网格提供了“窗口”,需要以这样的方式添加控件,使容器的变换适用于该控件(即网格的同级或子级)

How can I modify my ItemsTemplate , so that it instantiates a single control (border/rectangle/etc), independent of the Items? 如何修改我的ItemsTemplate ,以便实例化一个独立于Items的控件(边框/矩形/等)?

You cannot modify the children of a panel being used as the ItemsPanelTemplate 您不能修改用作ItemsPanelTemplate的面板的子级

What I usually do instead is put both the ItemsControl and the overlay control in an panel that allows its children to overlap, such as a Grid 我通常要做的是将ItemsControl和overlay控件放在允许其子项重叠的面板中,例如Grid

For example, 例如,

<ScrollViewer Name="timelineScrollViewer" ...>
    <Grid>

        <ItemsControl Name="KeyGridPresenter" ... >

        </ItemsControl>

        <Grid x:Name="SomethingDrawnOnTopOfItemsControl">
            ...
        </Grid>

    </Grid>
 </ScrollViewer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM