Windows Phone 8 LonglistSelector绑定数据上下文

马兹特

目前,我正在将itemsource绑定到longlistselector。我将如何绑定数据上下文,而不是下面的代码?

在我的cs文件中,问题是我需要为数据上下文绑定而不是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>

我的长列表选择器

   <phone:LongListSelector x:Name="bookslist" 
                                 Background="Transparent"  
                                 IsGroupingEnabled="False"
                                 ListFooterTemplate ="{StaticResource booksListFooter}"                        
                                 ItemTemplate="{StaticResource BooksItemTemplate}"/>
贝诺瓦·凯瑟琳

只需添加this.DataContext=bookcategoriesvm ;您的构造器并修改您的LongListSelector,如下所示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

LongListSelector不绑定数据数组Windows Phone 8

来自分类Dev

如何从Windows Phone 8中的xml文件绑定数据?

来自分类Dev

Windows Phone 8图像绑定

来自分类Dev

Windows Phone 8 longlistselector selectmore添加更多

来自分类Dev

单击LongListSelector Windows Phone 8中的按钮

来自分类Dev

TiltEffect对longListSelector项的影响,Windows Phone 8

来自分类Dev

单击LongListSelector Windows Phone 8中的按钮

来自分类Dev

Windows Phone绑定数据

来自分类Dev

Windows Phone 8绑定多个选择ListView

来自分类Dev

Windows Phone 8的本地数据库

来自分类Dev

在Windows Phone 8中解析xml数据

来自分类Dev

如何从Windows Phone 8中的数据绑定设置样式staticresource

来自分类Dev

数据绑定到int列表“ Windows Phone 8”

来自分类Dev

Windows Phone 8中的多级数据绑定

来自分类Dev

Windows Phone 8记录

来自分类Dev

Windows Phone 8的LockScreen

来自分类Dev

Windows Phone 8记录

来自分类Dev

如何在Windows Phone 8的LongListSelector中将数组的指定元素绑定到图像源

来自分类Dev

在Windows Phone 8中使用LongListSelector进行分页

来自分类Dev

Windows Phone 8:将数据从PHP页面传递到Windows Phone 8应用

来自分类Dev

获取上下文菜单Windows Phone的父控件

来自分类Dev

Windows Phone 8绑定列表到列表框内的列表

来自分类Dev

Windows Phone 8路径形状绑定通知问题

来自分类Dev

“ Windows Phone Silverlight 8”是否与“ Windows Phone 8”相同?

来自分类Dev

Windows Phone 8 Image Binding

来自分类Dev

Windows Phone 8:显示pdf

来自分类Dev

Windows Phone 8中的NavigationDrawer

来自分类Dev

Windows Phone 8蓝牙开发

来自分类Dev

Windows Phone 8-PriorityBinding