2017-11-26 14 views
2

ListViewでTextCellをクリックすると、その行が強調表示されます。コードでTextCellを選択しても行がハイライト表示されない

ただし、プログラムで行/テキストセルを選択すると、その行はハイライト表示されません。

したがって、行をタップして選択を変更しない限り、現在選択されているListViewの値をユーザーに示すことはできません。

これはバグですか、機能が欠けているのでしょうか、コードを使ってどのように強調表示できますか?

サンプルコードは以下に添付されています。あなたは、これはそれがAndroidとiOS上で正常に動作方法を見て、そのプラットフォーム上で、具体的バグのように見えるUWPフォームアプリでテストされているコメントで述べたように

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

使用しているフォームのバージョンがあり、行がハイライト表示されていないコードで再作成していないため、ページに.xamlコードがあります。 – Nick

+0

いいえ、それは普通のC#です.XAMLは必要ありません。 Xamarin.Forms 2.4.0.74863 – tmighty

+0

@ニック私は2.5.0.91635にアップデートしました。これは同じ動作です。 – tmighty

答えて

1

これを回避するには、ページコンストラクタの代わりにOnAppearingに選択した項目を設定して強調表示させることができました。

+0

ああ、私は決して自分でこの回避策を理解することはできませんでした。または、犯人が何であったかを認識することさえできます。あなたは本当に私を救った。どうもありがとうございます。 – tmighty

+0

私はあなたが "this.Content = _lv;"と言った後、 "_lv.SelectedItem = ..."を使用すると、あなたのソリューションは私のためにしか機能しないと付け加えたいと思います。おそらく、 ".Content ="がフォーカスの問題を引き起こし、それが次に目に見える選択をクリアするためでしょうか? – tmighty

+0

ListViewを実際にページのコンテンツに追加する前にListViewを作成しています。そのため、すべてのロジックをOnAppearingに移動してSelectedItemだけでなく、SelectedItemの設定を移動する必要があります。 – Nick

関連する問題