2012-07-09 90 views
0

當Windows Phone 7應用程序打開一個視圖時,爲了創建一個特定的業務順序。至於構造函數和事件去,我發現這個順序是正確的:如何呈現數據綁定視圖?

  1. Constructor
  2. OnNavigatedTo
  3. OnLoaded

但是,我在這裏,我需要進行數據綁定的位置一個ListListBox基本視圖(背景,其他元素等)已加載後。所以我需要知道什麼時候以及如何在數據綁定之前知道視圖已加載。

我試圖在OnLoaded -event上做到這一點,但好像在這裏做數據綁定 - 在它遍歷這些元素之後 - 它們似乎還沒有存在(VisualTreeHelper-class can似乎沒有找到節點)。所以如你所見,我被卡住了。

任何幫助將不勝感激。

編輯:根據要求,這裏是關於發生了什麼更多的信息。

我的List由一些自定義(不太複雜)的對象填充,包括異步加載的圖像(delay.LowProfileImageLoader提供)和矩形。

的XAML:

<ListBox x:Name="ChannelsListBox" ItemsSource="{Binding AllChannels}"> 
//... 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid x:Name="ChannelTile" Margin="6,6,6,6" Tap="ChannelTile_Tap" Opacity="0.4"> 
      <!-- context menu goes here --> 
      <Rectangle Width="136" Height="136" Fill="{StaticResource LightGrayColor}" /> 
      <Image Width="136" Height="136" delay:LowProfileImageLoader.UriSource="{Binding ImageUri}" /> 
     </Grid> 
    </DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

代碼隱藏:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    UpdateApplicationBar(); 

    pickChannelsViewModel = new PickChannelsViewModel(); 
    DataContext = pickChannelsViewModel; 

    if (hasUpdatedTiles) 
    { 
     pickChannelsViewModel.IsLoading = false; // Set by UpdateTiles() 
    } 
} 


private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    // This is where I would data bind the list (instead of in XAML) 
    UpdateTiles(); // Traverses the list and changes opacity of "selected" items. 
} 

protected void UpdateTiles() 
    { 
     foreach (var item in ChannelsListBox.Items) 
     { 
      if (pickChannelsViewModel.SelectedChannels.Contains(item as Channel)) 
      { 
       var index = ChannelsListBox.Items.IndexOf(item); 

       // This returns null when databinding in codebehind, 
       // but not in XAML 
       ListBoxItem currentItem = ChannelsListBox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; 

       if (currentItem != null && VisualTreeHelper.GetChildrenCount(currentItem) == 1) 
       { 
        var OuterWrapper = VisualTreeHelper.GetChild(currentItem, 0); 
        var MiddleWrapper = VisualTreeHelper.GetChild(OuterWrapper, 0); 
        var InnerWrapper = VisualTreeHelper.GetChild(MiddleWrapper, 0); 
        Grid currentItemGrid = VisualTreeHelper.GetChild(InnerWrapper, 0) as Grid; 

        currentItemGrid.Opacity = 1.0; 
       } 
      } 
     } 
     pickChannelsViewModel.IsLoading = false; 
     hasUpdatedTiles = true; 
    } 

的項目本身是在存儲器(取出從在應用程序的較早階段REST),因此應提供瞬間。

我想解決的問題是在這個特別的視圖上有一個相當長的加載時間(這裏約有140個項目正在創建,然後通過過濾和更改不透明度)。

回答

0

我相信你正在做的事情一樣:一旦你設置一個列表框在列表中的變化應該在任何時候ListBox中反映的的ItemSource

myListBox.ItemSource=myList; 

。如果ListBox爲空,原因必須是ListTemplate中沒有正確填充或無效Bindings。您應該調試並通過在Loaded()方法中插入斷點來查明您的List是否有任何項目。此外,您還沒有提到您的列表包含什麼項目,或者它在應用程序中的填充位置?不完整的信息不會幫助任何人。

+0

增加了一些信息 - 對不起,從一開始就沒有包括它。 – 2012-07-09 08:01:11