gpt4 book ai didi

c# - 通过代码选择 TextCell 不会突出显示行

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:40 25 4
gpt4 key购买 nike

当我单击 ListView 中的 TextCell 时,该行会突出显示。

但是,当我以编程方式选择一行/一个 TextCell 时,该行未突出显示。

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

这是错误还是缺少功能,或者我如何通过代码实现突出显示?

示例代码附在下面。

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; }
}
}

最佳答案

正如您在 UWP Forms 应用程序上测试的评论中提到的,这似乎是该平台上的一个错误,看看它在 Android 和 iOS 上如何正常工作。

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

关于c# - 通过代码选择 TextCell 不会突出显示行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com