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