如何将单个控件实例添加到ItemsPanelTemplate
本文关键字:添加 ItemsPanelTemplate 实例 控件 单个 | 更新日期: 2023-09-27 18:26:00
在我的项目中,我有一个ItemsControl
,它的ItemsPanel
模板是Grid
我想在这个网格中添加一个控件,它完全独立于ItemsControl"Items",可以在代码后面访问它最好的方法是什么
更多详细信息:
我正在WPF中实现一个关键帧动画时间轴。我通过将一组"指标"绑定到ItemsControl来实现这一点;ItemsControl然后使用Margin属性()根据它们的位置在网格上定位每一个
<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>
然后,这个指示器网格会自动调整到关键帧的大小,并使用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>
我想添加一个半透明的"荧光笔/插入符号",用户可以用鼠标控制它,但由于这种设计是基于网格是一个抽象的"表面",ItemsControl
和ScrollViewer
为其提供了一个"窗口",我需要添加控件,使容器的转换应用于它(即网格的兄弟或子对象)
如何修改我的ItemsTemplate
,使其实例化独立于Items的单个控件(border/矩形等)
不能修改用作ItemsPanelTemplate
的面板的子级
相反,我通常所做的是将ItemsControl
和覆盖控件都放在一个允许其子控件重叠的面板中,例如Grid
例如,
<ScrollViewer Name="timelineScrollViewer" ...>
<Grid>
<ItemsControl Name="KeyGridPresenter" ... >
</ItemsControl>
<Grid x:Name="SomethingDrawnOnTopOfItemsControl">
...
</Grid>
</Grid>
</ScrollViewer>