1

我已经有一个工作列表框,其中包含本地数据库中的项目。现在我想将其升级为 CollectionViewSource 进行过滤。升级后,带有 CollectionViewSource 的新 ListBox 什么也没有显示。

主页代码背后:

    // Data context for the local database
    private BuildingDataContext toDoDB;

    // Define an observable collection property that controls can bind to.
    private ObservableCollection<Building> _buildings;
    public ObservableCollection<Building> BuildingTable
    {
        get
        {
            return _buildings;
        }
        set
        {
            if (_buildings != value)
            {
                _buildings = value;
                NotifyPropertyChanged("BuildingTable");
            }
        }
    }

    public CollectionViewSource Source { get; set; }

    // Konstruktor
    public MainPage()
    {
        InitializeComponent();
        // Connect to the database and instantiate data context.
        toDoDB = new BuildingDataContext(BuildingDataContext.DBConnectionString);

        // Data context and observable collection are children of the main page.
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Define the query to gather all of the to-do items.
        var toDoItemsInDB = from Building todo in toDoDB.BuildingTable
        select todo;

        // Execute the query and place the results into a collection.
        BuildingTable = new ObservableCollection<Building>(toDoItemsInDB);

        Source = new CollectionViewSource();
        Source.Source = BuildingTable;

        // Call the base method.base.OnNavigatedTo(e);
    }

为此,我添加了以下几行:

public CollectionViewSource Source { get; set; }
Source = new CollectionViewSource();
Source.Source = BuildingTable;

我也试着把

Source = new CollectionViewSource();
Source.Source = BuildingTable;

在我的 MainPage 构造函数中。它也不起作用。

我的 Mainpage.xaml:

        <!--<ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding BuildingTable}" Grid.Row="0" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation">-->
        <ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding Source.View}" Grid.Row="0" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" Width="440">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Name="textBlockShortcut" Text="{Binding Shortcut}" Width="Auto" HorizontalAlignment="Left" Grid.Column="0" Margin="0,0,0,5" FontSize="36" />
                        <TextBlock Name="textBlockName" Text="{Binding BuildingName}" Width="Auto" HorizontalAlignment="Left" Grid.Column="1" Margin="0,0,0,5" FontSize="36" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

第一个注释行显示了旧的工作列表框,没有CollectionViewSource. 那么我错过了什么?

编辑:

    private void goToNavigation(object sender, RoutedEventArgs e)
    {
        // If selected index is -1 (no selection) do nothing
        if (toDoItemsListBox.SelectedIndex == -1)
            return;

        // Navigate to the new page
        PhoneApplicationService.Current.State["SelectedItem"] = toDoItemsListBox.SelectedItem;
        NavigationService.Navigate(new Uri("/NavigationPage.xaml", UriKind.Relative));

        // Reset selected index to -1 (no selection)
        toDoItemsListBox.SelectedIndex = -1;
    }
4

2 回答 2

2

您通常会在 XAML 中创建并绑定到 CollectionViewSource:

<UserControl.Resources>
    <CollectionViewSource x:Key="cvs"/>
</UserControl.Resources>
<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource cvs}}" ...>
        ...
    </ListBox>
</Grid>

并在代码隐藏中像这样访问 CollectionViewSource:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ... 
    var cvs = Resources["cvs"] as CollectionViewSource;
    cvs.Source = BuildingTable;
}
于 2013-01-19T19:26:31.883 回答
1

您不直接使用 CollectionViewSource 类,而是使用适当类型的 CollectionView。

View = CollectionViewSource.GetDefaultView( myCollection );

然后你将它直接绑定到你的源。

ItemsSource="{Binding View}"

您可以而且只能使用 xaml 中的 CollectionViewSource,因为这是它的主要目的。从代码中,您应该直接创建一个 CollectionView 或使用 GetDefaultView 方法。

于 2013-01-19T18:33:40.353 回答