2013-10-01 149 views
0

當前我綁定itemsource到longlistselector。我將如何在下面的代碼中綁定datacontext?windows phone 8 longlistselector綁定datacontext

在我的cs文件中,問題是我需要爲datacontext綁定而不是itemsource指定正確的語法,以便我的viewmodel中的兩個屬性DisplayShowMoreButton和BooksCategoriesList分別綁定到footertemplate和itemtemplate中的longlistselector。

public BooksListing() 
    { 
     InitializeComponent(); 
     bookcategoriesvm = new BookCategoriesViewModel(); 


    } 

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    {   
      base.OnNavigatedTo(e); 
      if (NavigationContext.QueryString.TryGetValue("catid", out categoryid)) 
      { 
       if (this.State.ContainsKey("categoryid")) 
       { 
        categoryid = this.State["categoryid"].ToString(); 
       } 
      }   
      bookcategoriesvm.GetPagedBookCategoriesList(Convert.ToInt64(categoryid), 0); 
      bookslist.ItemsSource = bookcategoriesvm.BooksCategoriesList;   
    } 

這是我的觀點模型。基本上DisplayShowMoreButton和BooksCategoriesList是兩個獨立的實體。你可以在下面找到它們。

public class BookCategoriesViewModel : ViewModelBase 
{ 
    public Paging<BookCategories> paging; 
    public int Pagesize = 5;   
    public BookCategoriesRepository bookcategoriesrepository = new BookCategoriesRepository(); 



    private ObservableCollection<BookCategories> _bookscategorieslist { get; set; } 
    public ObservableCollection<BookCategories> BooksCategoriesList 
    { 
     get { return _bookscategorieslist; } 
     set 
     {     
      _bookscategorieslist = value;  
     } 
    } 




    public string _DisplayShowMoreButton = "Visible"; 
    public string DisplayShowMoreButton 
    { 
     get 
     { 
      return _DisplayShowMoreButton; 
     } 
     set 
     { 
      if (RecordCount <= paging.RequiredListcount) 
      { 
       _DisplayShowMoreButton = "Collapsed"; 
      } 
      else 
      { 
       _DisplayShowMoreButton = "Visible"; 
      } 
      OnPropertyChanged("DisplayShowMoreButton"); 
     } 
    } 

} 

這是我的xaml文件。我在DisplayShowMoreButton需要綁定的頁面下方顯示更多按鈕(頁腳模板)和BooksCategoriesList列表,這些列表需要綁定到項目模板。

<phone:PhoneApplicationPage.Resources> 

    <DataTemplate x:Key="booksListHeader"> 
     <Border Background="Purple"> 
      <TextBlock Text="Books Header" /> 
     </Border> 
    </DataTemplate> 
    <DataTemplate x:Key="booksListFooter"> 
     <StackPanel>// binding problem here 
      <Button Content="Show More" x:Name="showmorebutton" Click="showmorebutton_Click" Visibility="{Binding DisplayShowMoreButton,Mode=OneWay}" /> 
     </StackPanel> 


    </DataTemplate> 

    <DataTemplate x:Key="BooksItemTemplate"> 
     <Grid x:Name="GridBox" Grid.Row="1" Margin="0,0,0,0"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Image Name="loadingImage" Width="125" Source="Images/imageloading.jpg" Height="220" VerticalAlignment="Top"/> 
      <Button Name="thbbtn" BorderThickness="0" Tag="{Binding BookId,Mode=OneWay}" Margin="0,-20,0,0" Click="thbbtn_Click" > 
       <Image Name="ThumbnailImage" Width="125" Source="{Binding Images,Mode=OneWay, Converter={StaticResource ImageConverter}}" Height="220" VerticalAlignment="Top"/> 
      </Button> 
      <StackPanel Grid.Column="1" Grid.Row="0" VerticalAlignment="Top"> 
       <TextBlock Name="booktitle" Text="{Binding BookTitle,Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/> 
       <TextBlock Text="{Binding AuthorName,Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiLight}"/> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources> 

我longlistselector

<phone:LongListSelector x:Name="bookslist" 
           Background="Transparent" 
           IsGroupingEnabled="False" 
           ListFooterTemplate ="{StaticResource booksListFooter}"       
           ItemTemplate="{StaticResource BooksItemTemplate}"/> 

回答

1

只是在你的構造器添加this.DataContext=bookcategoriesvm ;和修改LongListSelector這樣的:

<phone:LongListSelector x:Name="bookslist" ListFooter="{Binding }" ItemsSource="{Binding BooksCategoriesList}" ..../> 
相關問題