2009-08-11 76 views
0

我需要一個 ,以填補一個項目列表框喜歡充滿一個項目的列表1/5秒之後的其他項目將被添加到列表WPF數據綁定列表框

任何想法如何可以完成(在WPF)?

回答

2

如果綁定列表框到ObservableCollection<T>,你只能從UI線程修改集合。所以,你可以使用一個DispatcherTimer,這引起了在UI線程上的Tick事件,或使用專門收集像this one,並從另一個線程

0

在窗口或控制的Loaded事件,執行通過調用System.Windows.Threading.DispatcherObject爲 UI元素BeginInvoke方法來加載數據 的第一項,並指定一個系統中的方法。 Windows.Threading.DispatcherPriority的背景。當 該方法已完成生成數據並將其添加到列表中時,遞歸地將Dispatcher的隊列添加到 相同的方法,每次只添加一個項目,然後排隊呼叫 以添加具有DispatcherPriority背景的下一個。

private ObservableCollection<string> numberDescriptions; 
// Declare a delegate to wrap the LoadNumber method 
private delegate void LoadNumberDelegate(int number); 
private void LoadNumber(int number) 
{ 
// Add the number to the observable collection 
// bound to the ListBox 
numberDescriptions.Add("Number " + number.ToString()); 
if(number < 10000) 
{ 
// Load the next number, by executing this method 
// recursively on the dispatcher queue, with 
// a priority of Background. 
// 
this.Dispatcher.BeginInvoke(
DispatcherPriority.Background, 
new LoadNumberDelegate(LoadNumber), ++number); 
} 
} 
private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
// Initialize an ObservableCollection of strings 
numberDescriptions = 
new ObservableCollection<string>(); 
// Set it as the ItemsSource for the ListBox 
listBox.ItemsSource = numberDescriptions; 
// Execute a delegate to load 
// the first number on the UI thread, with 
// a priority of Background. 
// 
this.Dispatcher.BeginInvoke(
DispatcherPriority.Background, 
new LoadNumberDelegate(LoadNumber), 1); 
} 

看到WPF Recipes in C# 2008負荷的項目在ListBox異步(第460)

0

在此處填寫是如何用的DataTemplate RSS提要的列表框綁定的例子:

<UserControl.Resources> 
     <XmlDataProvider x:Key ="DataRSS" XPath="//item" Source="http://rss.feedsportal.com/c/629/f/502199/index.rss"></XmlDataProvider > 
    </UserControl.Resources > 

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 

      <ListBox ItemsSource="{Binding Source={StaticResource DataRSS}}" Height="516" Margin="0,0,32,0" Background="{x:Null}" BorderBrush="#FF627DAE"> 
       <ListBox.ItemTemplate > 
        <DataTemplate > 
         <Grid Width="400" Height="100" >         

          <Image Source="{Binding XPath=enclosure/@url}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" /> 
          <TextBlock TextWrapping="Wrap" Text="{Binding XPath=title}" FontWeight="Bold" Grid.Column="2"/> 
         </Grid> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </StackPanel> 
</grid>