<Window x:Class="wpfDemo.DataBind.ComboxBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ComboxBinding" Height="300" Width="300">
<Grid>
<ComboBox Height="23" Margin="12,12,0,0" Name="cmbLitter" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,83,0,0" Name="cmbMore" VerticalAlignment="Top" Width="120" />
<ComboBox x:Name="cmbMoreLarge" Margin="12,0,0,90" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="120">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<Label Height="23" Margin="138,12,60,0" Name="label1" VerticalAlignment="Top">20Item</Label>
<Label Height="23" Margin="138,83,60,0" Name="label2" VerticalAlignment="Top">2000Item</Label>
<Label Height="23" Margin="138,0,60,90" Name="label3" VerticalAlignment="Bottom">2000Item</Label>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace wpfDemo.DataBind
{
/// <summary>
/// ComboxBinding.xaml 的交互逻辑
/// </summary>
public partial class ComboxBinding : Window
{
public ComboxBinding()
{
InitializeComponent();
bind();
}
private void bind()
{
this.cmbLitter.ItemsSource = getSource(20);
this.cmbLitter.DisplayMemberPath = "Name";
this.cmbLitter.SelectedValuePath = "Id";
this.cmbLitter.SelectedIndex = 0;
this.cmbMore.ItemsSource = getSource(2000);
this.cmbMore.DisplayMemberPath = "Name";
this.cmbMore.SelectedValuePath = "Id";
this.cmbMore.SelectedIndex = 0;
this.cmbMoreLarge.ItemsSource = getSource(2000);
this.cmbMoreLarge.DisplayMemberPath = "Name";
this.cmbMoreLarge.SelectedValuePath = "Id";
this.cmbMoreLarge.SelectedIndex = 0;
}
private List<User> getSource(int length)
{
List<User> list = new List<User>();
for (int i = 0; i < length; i++)
{
User u = new User();
u.Id = i;
u.Name = "admin" + i;
list.Add(u);
}
return list;
}
}
}