ItemsPanelTemplate内のCanvasオブジェクトにアクセスしようとしているので、コードビハインドでアイテムを直接追加できます。
これが私のXAMLです。
<ListBox x:Name="MyMap" ItemsSource="{Binding MapItems}"
Background="Gray"
SelectionChanged="MyMap_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="MyMapCanvas"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="Transparent">
</Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
コードビハインドの「MyMapCanvas」にアクセスしてアイテムを追加するにはどうすればよいですか?
私は以下を試しました:
Canvas canvas = (Canvas)MyMap.ItemsPanel.LoadContent();
と
ContentPresenter cp = (ContentPresenter)(VisualTreeHelper.GetChild(MyMap, 0));
ItemsPanelTemplate ipt = MyMap.ItemsPanel;
Canvas canvas = ipt.FindName("MyMapCanvas", cp) as Canvas;
前もって感謝します。