0

currently i am binding itemsource to the longlistselector. how would i going to bind the datacontext instead in the code below?

in my cs file the problem is that i need to specify the correct syntax for datacontext binding instead of itemsource so that both of my properties DisplayShowMoreButton and BooksCategoriesList in my viewmodel are binded to the longlistselector in the footertemplate and itemtemplate respectively.

 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;         
    }

here is my view model. basically DisplayShowMoreButton and BooksCategoriesList are two separate entities. you can find them below.

  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");
        }
   } 

}

here is my xaml file. i have a show more button below where DisplayShowMoreButton needs to be bonded(footer template) and BooksCategoriesList list which needs to be binded to the item template.

  <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>

my longlistselector

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

1 回答 1

1

只需添加this.DataContext=bookcategoriesvm ;您的构造函数并像这样修改您的 LongListSelector :

<phone:LongListSelector x:Name="bookslist" ListFooter="{Binding }"  ItemsSource="{Binding BooksCategoriesList}"  ..../>
于 2013-10-01T11:42:57.507 回答