当我在TextCell中单击ListView时,该行将被高亮显示。
但是,当我以编程方式选择行/a TextCell时,该行不会突出显示。
因此,除非用户通过点击一行来更改所选内容,否则不可能向用户指示当前选择了ListView中的哪个值。
这是一个bug还是一个缺失的特性,或者我如何通过代码实现高亮显示?
示例代码附在下面。
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; }
}
}
发布于 2017-11-26 22:48:48
正如评论中提到的,您正在UWP窗体应用程序上进行测试,这似乎是一个bug,特别是在该平台上,看看它在Android和iOS上是如何工作的。
我能够通过在OnAppearing
中而不是在页面构造函数中设置所选的项来使其突出显示来解决这个问题。
https://stackoverflow.com/questions/47498096
复制相似问题