【问题标题】:How to select a list view item programatically如何以编程方式选择列表视图项
【发布时间】:2019-03-12 07:54:48
【问题描述】:

我正在制作图书馆管理软件。当用户点击一本书时,我想显示关于这本书的完整信息。 Book 类型的属性之一是标签。因为一本书可能有很多标签,所以我决定使用列表视图来显示标签。

我想在列表视图中选择标签。我该怎么做?我找到了this question。接受的答案说使用一种方法ListView.Select(),不幸的是它不存在。此外,ListView.Items[0].Selected = true; 不会编译。这是错误:

错误 CS1061“object”不包含“Selected”的定义,并且找不到接受“object”类型的第一个参数的可访问扩展方法“Selected”(您是否缺少 using 指令或程序集引用?)

编辑:有人要代码。在这里。

这是列表视图:

<ListView x:Name="TagsListView"
                  SelectionMode="Multiple"
                  ItemsSource="{x:Bind Tags}"
                  Grid.Row="4"
                  Grid.Column="1"/>

这是后面的代码:

public sealed partial class BookInfo_View : Page
{
    //don't worry about DataAccess. 
    private Book book = new Book();
    private List<string> Tags = DataAccess.GetTags();

    public BookInfo_View()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        book = (Book)e.Parameter;

        //dont worry about how this works. This line of code gives me the tags
        string[] selectedTags = book.Tags.Split(';', System.StringSplitOptions.RemoveEmptyEntries);

        //here i want to select the selected tags
    }
}

这是书类:

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string Publisher { get; set; }
    public string ISBN { get; set; }

    public int Quantity { get; set; }

    public string CoverImageLocation { get; set; }

    public string Tags { get; set; }
}

编辑:我觉得你们没有理解这个问题。问题是

ListView.Items[0].Selected = true;

上面这行代码无法编译!它给出了上面提到的错误

【问题讨论】:

标签: c# listview uwp


【解决方案1】:

当您正在更新OnNavigatedTo 中的ListView 选定项目时,您的ListView 可能没有使用当时的数据初始化,请尝试在页面Loaded 事件中更新您的ListView,如下所示。

public sealed partial class BookInfo_View : Page
{
    //don't worry about DataAccess. 
    private Book book = new Book();
    private List<string> Tags = DataAccess.GetTags();
    private string[] selectedTags;

    public BookInfo_View()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        book = (Book)e.Parameter;

        //dont worry about how this works. This line of code gives me the tags
        selectedTags = book.Tags.Split(';', System.StringSplitOptions.RemoveEmptyEntries);

        //here i want to select the selected tags
        this.Loaded += OnPageLoaded;
    }

    private void OnPageLoaded(object sender, RoutedEventArgs e)
    {
       foreach (string selectedTag in selectedTags)
       {
         TagsListView.SelectedItems.Add(selectedTag);
       }
    }
}

【讨论】:

  • 我觉得 this.Loaded += OnpageLoaded 应该在构造函数中,因为构造函数只会被调用一次,但 OnNavigateTo() 可能会被多次调用
  • @Hemil 是的,您是正确的,或者您可以在OnNavigatedFrom 中取消注册。
【解决方案2】:

ListView.Select() 只是激活控件(为其设置焦点)而不选择其项目,因此它不是您要查找的内容。

但是你已经提到的这个是你问题的答案:

ListView1.Items[0].Selected = true;

但是,您应该使用现有 ListView 实例的 Name 属性,而不是 ListView 本身。如果您没有重命名它,它应该类似于listView1

【讨论】:

    【解决方案3】:

    基于this,我认为您应该将标签添加到 TagsListView.SelectedItems

    【讨论】:

    • 我这样做了foreach(string selectedTag in selectedTags) { TagsListView.SelectedItems.Add(selectedTag); }。它没有突出显示列表视图项
    猜你喜欢
    • 2016-06-16
    • 2013-03-30
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多