0
WPF MVVMパターンでは、モデルデータのみがItemsControlにバインドされています その項目のUIElementを取得できますか?WPF MVVMパターンでは、モデルデータはItemsControlにのみバインドされています。その項目のUIElementを取得できますか?
しかし、何百ものItemsControlがあるので、ItemsControlには が含まれていますがわかりません。
void InitData()
{
GroupList = new ObservableCollection<GroupModel>();
for (int i = 0; i < 10; i++)
{
GroupModel groupItem = new GroupModel() { GroupName = $"Group {i}" };
groupItem.ItemList = new ObservableCollection<KeyValueModel>();
for (int i2 = 0; i2 < 100; i2++)
{
groupItem.ItemList.Add(new KeyValueModel() { Key = string.Format("Key {0}", i2.ToString("000")), Value = string.Format("Value {0}", i2.ToString("000")) });
}
GroupList.Add(groupItem);
}
// Can I get the UIElement of the targetItem?
KeyValueModel targetItem = GroupList.Last().ItemList.Last();
}
<ItemsControl ItemsSource="{Binding GroupList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding GroupName}" Foreground="Black" FontSize="24" FontWeight="Bold" Margin="12,0,12,0" />
<ItemsControl ItemsSource="{Binding ItemList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Black" Width="100" Height="100" Margin="0,0,10,10">
<TextBlock Text="{Binding Key}" Foreground="White" />
<TextBlock Text="{Binding Value}" Foreground="White" Margin="10,0,0,0" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ここで、UI要素とどのUI要素への参照を取得したいですか? – mm8
なぜUIアイテムを取得したいのですか?そして、あなたの 'WrapPanel'はパフォーマンスを低下させるかもしれない'仮想化 'をサポートしていません。 – XAMlMAX