windows phone 8 longlistselector绑定datacontext

时间:2013-10-01 10:34:26

标签: windows-phone-7 windows-phone-8 windows-phone

目前我将itemsource绑定到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 个答案:

答案 0 :(得分:1)

只需在构造函数中添加this.DataContext=bookcategoriesvm ;并修改LongListSelector,如下所示:

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