Generally, we are binding in WPF Combox following such an approach.
XAML:
<ComboBox IsEditable="False" ItemsSource="{Binding List, Mode=OneWay, IsAsync=True}" SelectedIndex="{Binding SelectedSheetIndex, Mode=TwoWay}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="2.5" Text="{Binding Name, Mode=OneTime}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
Backstage:
private List<string> list; public List<string> List { get { return list; } set { SetProperty(ref list, value); } }
Problem: will Caton, click on the drop-down list can not eject large amounts of data binding.
Solution is toItemsPanelTemplate的值设置为VirtualizingStackPanel。它可以让UI显示1万个左右的项目。
<ComboBox IsEditable="False" ItemsSource="{Binding SheetList, Mode=OneWay, IsAsync=True}" SelectedIndex="{Binding SelectedSheetIndex, Mode=TwoWay}"> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </ComboBox.ItemsPanel> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="2.5" Text="{Binding Name, Mode=OneTime}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>