绑定到CollectionViewSource的LongListSelector显示空列表

时间:2014-03-15 17:57:28

标签: c# wpf xaml windows-phone-8

我尝试在我的项目中使用CollectionViewSource,当它没有工作时,我创建了一个新的空项目来简化操作。现在问题是LLS是空的。我查看了事件处理程序e.Accepted实际评估为true。可能是我错过了什么。

我理解的是传递给事件处理程序的每个Item,其中Accepted属性为true,显示在列表中,我是对的吗?

MainPage.xaml中:

<phone:PhoneApplicationPage
x:Class="Filtered_List_Test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"  Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Name="FilteredList" Source="{Binding Items}" Filter="FilteredList_Filter" />
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

    <!--Pivot Control-->
    <phone:Pivot Title="MY APPLICATION">

        <!--Pivot item one-->
        <phone:PivotItem Header="first">
            <!--Double line list with text wrapping-->
            <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Source={StaticResource FilteredList}}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17">
                            <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>

    </phone:Pivot>

</Grid>

</phone:PhoneApplicationPage>

MainPage.xaml.cs中:

namespace Filtered_List_Test
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    // Load data for the ViewModel Items
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }

    private void FilteredList_Filter(object sender, System.Windows.Data.FilterEventArgs e)
    {
        e.Accepted = true;
    }
}
}

基于标准的PivotApp模板构建。

UPD:逐行进一步调试显示,由于某种原因View CollectionViewSource属性未传播。 Source显示所有商品,但View停留null。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

经过进一步研究后,我发现LongListSelector无法绑定到CollectionViewSource。您必须使用ListBox才能绑定到CollectionViewSource