How to make itemtemplate aware of its containing template?

前端 未结 1 959
挽巷
挽巷 2021-01-23 23:27

I want this Ellipse to get its coordinates from its corresponding BallViewModel, and to use them to determine its location inside a canvas. The list of balls is bound to L

相关标签:
1条回答
  • 2021-01-24 00:02

    When you use an ItemTemplate with an ItemControls it does not directly put your Elippses on the Canvas but wraps them into an ContentPresenter. So you have to apply your canvas.Bottom/Left properties on the ItemsPresenter. You can can do this with the ItemContainerStyle:

    <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Bottom" Value="{Binding Y}" />
                    <Setter Property="Canvas.Left" Value="{Binding X}" />                    
                </Style>
     </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type VM:BallVM}">
                    <Ellipse Width="100" Height="100" Fill="Red"/>
                </DataTemplate>
     </ItemsControl.ItemTemplate>
    
    0 讨论(0)
提交回复
热议问题