2017-10-22 41 views
0

ピッカーにバインドされた選択項目を取得するのに問題があります。私は様々な回答をしてきましたが、どれも私のために働いていません。ここに私のコードXamarinフォームSelectedItemがピッカーに表示されない

私のXAML

`<Picker x:Name="mobListPicker" 
    Style="{StaticResource PickerOrangeStyle}" Title="Select Mob" 
      
    HeightRequest="50" ItemsSource="{Binding ProductList, Mode=TwoWay}" 
    ItemDisplayBinding="{Binding ProductNo}" 
     
    SelectedItem="{Binding SelectedProduct,Mode=TwoWay}"> 
    
</Picker>` 

財産誰かが私には、エラーの特定に役立つことができればそれは素晴らしいだろう、選択した項目

private Product _selectedProduct; 
    public Product SelectedProduct 
    { 
     get { return _selectedProduct; } 
     set { SetProperty(ref _selectedProduct, value); } 
    } 

ためです。

+0

モデルとビューモデルも投稿する必要があります –

+0

ItemsSourceでModeを削除し、SelectedItemでUpdate Mode = Defaultを削除できますか – Kowalski

答えて

0

ピッカーとMVVMバインディングの使い方について簡単なデモプロジェクトを作成しました。アイテムソースとしてのObservableCollectionを使用します。

ユーザーがピッカーからアイテムの1つを選択すると、値/ラベルの内容が変更されます。私はこのプロジェクトがあなたの問題を解決するために必要なものだと思う。

あなたは私たちにあなたの全体のコードを提供していないので、あなたの可能性のあるエラーを見るのは難しいでしょう。たぶん、これはあなたのために役立つことがあります。

デモ:

enter image description here

コードスニペット:

私のPersonクラスは次のようになります。

public class Person { 

    public int PersonId { get; set; } 

    public string FName { get; set; } 
    public string LName { get; set; } 

    public string FullName { get { return $"{FName} {LName}"; } } 
    public string Biography { get; set; } 

} 

私のViewModel:

public class AllPersonsViewModel : INotifyPropertyChanged{ 

    public ObservableCollection<Person> AllPersons { get; set; } 

    private Person selectedPerson; 
    public Person SelectedPerson { 
     get { return selectedPerson; } 
     set { selectedPerson = value; 
      OnPropertyChanged(); 
     } 
    } 

    public AllPersonsViewModel() { 

     AllPersons = new ObservableCollection<Person>() { 

      new Person() { 
       Biography = "Lorem ipsum person 1", 
       FName = "Person", 
       LName = "One", 
       PersonId = 1, 
      }, 

      new Person() { 
       Biography = "Lorem ipsum person 2", 
       FName = "Person", 
       LName = "Two", 
       PersonId = 2, 
      }, 

      new Person() { 
       Biography = "Lorem ipsum person 3", 
       FName = "Person", 
       LName = "Three", 
       PersonId = 3, 
      }, 

     }; 


    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged([CallerMemberName]string propertyName = "") => 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
} 

私はBindingContext

として、このビューモデルを使用して1つのビューを持っている...とPicker制御と私のページのXAMLはここにある:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="XF.Picker.Demo.Views.PersonsPage"> 
    <ContentPage.Content> 
     <StackLayout Orientation="Vertical"> 

      <Picker Title="All person" 
        ItemDisplayBinding="{Binding FullName}" 
        ItemsSource="{Binding AllPersons}" 
        SelectedItem="{Binding SelectedPerson}"/> 

      <!-- Margin just to see picker dropdown and labels at the same time--> 
      <Label Text="{Binding SelectedPerson.FullName}" Margin="0,80,0,0"/> 
      <Label Text="{Binding SelectedPerson.Biography}"/> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

私はあなたがそれを見つけることができますgithubの上でこのコードプロジェクトを公開しhere

関連する問題