2012-07-09 52 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

相关问题