【问题标题】:How to pre-select multiple listview/gridview items in C#/XAML windows 8 app?如何在 C#/XAML windows 8 应用程序中预先选择多个列表视图/网格视图项目?
【发布时间】:2012-09-25 02:01:29
【问题描述】:

在我的应用程序中有我的自定义类的网格视图。我正在使用自定义数据模板,并且值是从 SQLite 绑定的。现在,当用户启动应用程序时,应在 gridview/listview 中预先选择某些项目(不是单个)。 Gridview/listview 允许多选。如何使用 SelectedItem 属性实现此目的?

更新:我关注了this,它对我不起作用。返回 0 个选择。

更新 2:我已经发布了代码

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
        lvTags.ItemsSource = db.Table<Database.Tag>();  //lvTags is listview

        if (MyList.Count > 0) //MyList is the static list of class "Database.Tag"
        {
            foreach (var item in MyList)
                foreach (var lvitem in lvTags.Items)
                    if (lvitem.Equals(item))
                        lvTags.SelectedItems.Add(lvitem);
        }
    }
}

更新 3:

public override bool Equals(object obj)
{
    Tag tag = obj as Tag;
    if (this.TagID == tag.TagID && this.TagName == tag.TagName)
        return true;
    else
        return false;
}

【问题讨论】:

    标签: c# xaml gridview microsoft-metro windows-runtime


    【解决方案1】:

    终于得到MSDN的答复。谢谢ForInfo

    XAML 页面

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="listView" SelectionMode="Multiple">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="{Binding ID}" Margin="0,0,5,0"/>
                        <TextBox Text="{Binding Title}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

    C#

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            LoadData();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        ObservableCollection<KiwiItem> sourceColl;
        IList<KiwiItem> selectionList;
        public void LoadData()
        {
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
    
            // Exec (1)
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                db.DropTable<KiwiItem>();
                db.CreateTable<KiwiItem>();
                db.RunInTransaction(() =>
                {
                    db.Insert(new KiwiItem() { ID = 1, Title = "MyTitle1" });
                    db.Insert(new KiwiItem() { ID = 2, Title = "MyTitle2" });
                    db.Insert(new KiwiItem() { ID = 3, Title = "MyTitle3" });
                    db.Insert(new KiwiItem() { ID = 4, Title = "MyTitle4" });
                });
                this.sourceColl = new ObservableCollection<KiwiItem>();
                this.selectionList = new List<KiwiItem>();
                // Query the db. In practice, fill the sourceColl according to your business scenario
                foreach (KiwiItem item in db.Table<KiwiItem>())
                {
                    this.sourceColl.Add(item);
                    if (item.ID == 2 || item.ID == 4)
                        this.selectionList.Add(item);
                }
            }
    
            // Exec (2)
            this.listView.ItemsSource = this.sourceColl;
            foreach (KiwiItem item in this.selectionList)
                this.listView.SelectedItems.Add(item);
        }
    }
    public class KiwiItem
    {
        [SQLite.AutoIncrement, SQLite.PrimaryKey]
        public int ID { get; set; }
        public string Title { get; set; }
    }
    

    【讨论】:

    • 我做了同样的事情,但在我的情况下不起作用,我不知道为什么?我实际上在做的是从 GridView 中选择一些项目,然后返回上一页,然后返回上一页,因此应该选择以前选择的项目,这不会发生。你能帮帮我吗?
    【解决方案2】:

    您可以使用 SelectedItems 属性。

        //
        // Summary:
        //     Gets the currently selected items.
        //
        // Returns:
        //     A collection of the currently selected items.
        public IList<object> SelectedItems { get; }
    

    【讨论】:

    • 为此 +1,虽然此集合是只读的,但不能进行数据绑定,您必须使用预先选择的项目手动填充它。
    • 但是如何将它与手动预选的项目进行数据绑定?我只有来自 SQLite 数据库的SelectedValuePath
    【解决方案3】:

    您可以使用 SelectedItems 属性并调用 SelectedItems.Add() 或 SelectedItems.Remove() 从选择中添加/删除项目。

    如果您在 GridView 上使用 ItemsSource 绑定,则可以使用 WinRT XAML 工具包中的 ListViewExtensions.BindableSelection 附加属性(它也应该与 GridView 一起使用,因为它是 ListViewBase 的子类),如 sample page 中一样。

    【讨论】:

    • protected override void OnNavigatedTo(NavigationEventArgs e) { foreach (var item in MyList) //MyList is the list of my custom class having integer and string property. { //It contains the object which will be preselected in listview. MyListView.SelectedItems.Add(item); } } 这个我试过了,还是不行。
    • 我也试过this
    • 尝试在原始问题中分享更多代码,包括 XAML、数据上下文、ItemsSource 绑定等。
    • 也许您的列表之一中的 Database.Tag 永远不会等于另一个列表中的任何 Database.Tag。您可能应该为您的项目视图模型使用与从数据库中获得的不同类型。
    • 对象是相似的,在if ((Database.Tag)lvitem == item)两边的值相同
    【解决方案4】:

    我删除了我原来的答案,因为你没有使用数据绑定,我的答案对你没有用。

    我刚刚发现这个可能对你有用:

    "SelectedItems 属性是只读的,不能直接设置"

    因此,对于可能提供帮助的解决方案,请参阅 this 文章。

    【讨论】:

    • 我没有使用单独的 ListBoxItem(或 GridViewItem)。我直接从 SQLite 的 Query&lt;T&gt;() 方法绑定项目源。
    • 根据您的评论更新了我的答案。
    • 谢谢,但我没有使用 MVVM,我的应用程序很简单。你能给我一个简单的演示应用程序吗,它在 gridview 中包含几个项目,并且在应用程序启动时会选择一些项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多