首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >按代码选择TextCell不会突出显示行

按代码选择TextCell不会突出显示行
EN

Stack Overflow用户
提问于 2017-11-26 16:02:34
回答 1查看 29关注 0票数 2

当我在TextCell中单击ListView时,该行将被高亮显示。

但是,当我以编程方式选择行/a TextCell时,该行不会突出显示。

因此,除非用户通过点击一行来更改所选内容,否则不可能向用户指示当前选择了ListView中的哪个值。

这是一个bug还是一个缺失的特性,或者我如何通过代码实现高亮显示?

示例代码附在下面。

代码语言:javascript
代码运行次数:0
运行
复制
using MyApp.Model;
using System.Collections.Generic;
using Xamarin.Forms;

namespace MyApp
{
    public class IntSelector : ContentPage
    {
        private ListView m_ListView;

        public IntSelector(int uSelectedInt)
        {
            DataTemplate nTemplate = new DataTemplate(typeof(TextCell));

            // We can set data bindings to our supplied objects.
            nTemplate.SetBinding(TextCell.TextProperty, "String");
            nTemplate.SetBinding(TextCell.DetailProperty, "Int");

            List<clsStringInt> nList = new List<clsStringInt>();

            clsStringInt nItem1 = new clsStringInt { String = "German", Int = 1031 };
            clsStringInt nItem2 = new clsStringInt { String = "English", Int = 1033 };
            clsStringInt nItem3 = new clsStringInt { String = "Spanish", Int = 1034 };

            nList.Add(nItem1);
            nList.Add(nItem2);
            nList.Add(nItem3);

            m_ListView = new ListView();
            m_ListView.ItemTemplate = nTemplate;
            m_ListView.ItemsSource = nList;

            m_ListView.ItemSelected += this.OnSelection;

            m_ListView.SelectedItem = nItem2;//this triggers the "OnSelection" event, so it works
            nItem2.String = "->> " + nItem2.String; //the item's new string is display in the ListView, so that works as well
            //what DOESN'T work is the highliting

            this.Content = m_ListView;
        }

        void OnSelection(object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem == null)
            {
                return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
            }

            clsStringInt n = (clsStringInt)e.SelectedItem;
            string sSelectedIntAsString = n.Int.ToString();

            DisplayAlert("Item Selected", sSelectedIntAsString, "Ok");
        }

    }
}

namespace MyApp.Model
{
    public class clsStringInt
    {
        public string String { get; set; }
        public int Int { get; set; }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-26 22:48:48

正如评论中提到的,您正在UWP窗体应用程序上进行测试,这似乎是一个bug,特别是在该平台上,看看它在Android和iOS上是如何工作的。

我能够通过在OnAppearing中而不是在页面构造函数中设置所选的项来使其突出显示来解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47498096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档