在 WPF MVVM 模式中,我只有绑定到 ItemsControl 的模型数据 我可以获取该项目的 UIElement 吗?
In the WPF MVVM pattern, I only have the model data bound to the ItemsControl Can I get the UIElement for that item?
在 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>
ItemsControl.ItemContainerGenerator
或许可以帮助你实现,请参考:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.itemscontrol.itemcontainergenerator.
在 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>
ItemsControl.ItemContainerGenerator
或许可以帮助你实现,请参考:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.itemscontrol.itemcontainergenerator.