0
在WPF MVVM模式中,我只有模型数据绑定到ItemsControl 我可以获得该项目的UIElement吗?在WPF MVVM模式中,我只有绑定到ItemsControl的模型数据我可以获得该项目的UIElement吗?
但是,因为有数百个ItemsControls,其中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'不支持'Virtualisation',这可能会导致性能下降。 – XAMlMAX