我綁定IList<T>
到列表框中,並期望它僅加載所需要的數據(可視)設置列表框VirtualizationMode(數據)
<Window.Resources>
<Style x:Key="lStyle" TargetType="{x:Type ListBox}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible"/>
</Style>
</Window.Resources>
<Grid Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ListBox Name="listbox1" ItemsSource="{Binding}" Grid.Column="0" Grid.Row="0"
Style="{DynamicResource lStyle}" Height="165" Margin="0,0,0,98" Width="296">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Width="100" Content="{Binding Path=Name}"></Label>
<Label Width="100" Content="{Binding Path=Age}"></Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
class MyCollection<T> : IList<T> where T : class //Just the relevant part
{
private List<T> _list = new List<T>();
public List<T> List
{
get { return _list; }
set { _list = value; }
}
public T this[int index]
{
get
{
Trace.WriteLine("Index: " + index);
return _list[index];
}
set
{
throw new NotImplementedException();
}
}
MyCollection<Person> mycollection1;
public Window1()
{
InitializeComponent();
mycollection1 = new MyCollection<Person>();
for (int i = 0; i < 100; i++)
{
mycollection1.Add(new Person { Name = "Tom", Age = 33 });
}
this.DataContext = mycollection1;
}
當我運行應用程序,索引被引用100次(每10項目在列表中),但它應該只爲列表中的項目調用。
爲什麼我不能使用IList?我可以使用任何其他接口嗎? – 2011-05-05 18:05:56
爲什麼所有指數都被觸發? – 2011-05-05 18:10:47
嗯,WPF檢查IList(沒有泛型),IList不是IList。將IList添加到MyCollection也應該可以。 –
kowd
2011-05-05 18:14:35