2017-07-14 5 views
0

リストに項目を追加する簡単なアプリケーションを作成してみてください。 ViewModelのプロパティを持つエントリのバインドに問題があります。双方向モードでエントリとプロパティをバインドできません

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:viewModels="clr-namespace:MyScoreDemo.ViewModels;assembly=MyScoreDemo" 
     x:Class="MyScoreDemo.Views.ClubListPage"> 
<StackLayout 
    Padding="15" 
    Spacing="10"> 

    <Grid> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <Label Text="Name:" Grid.Row="0" Grid.Column="0"/> 
     <Entry x:Name="EntryName" Text="{Binding Club.Name, Mode=OneWayToSource}" Grid.Row="0" Grid.Column="1"/> 

     <Label Text="Country:" Grid.Row="1" Grid.Column="0"/> 
     <Entry x:Name="EntryCountry" Text="{Binding Club.Country, Mode=OneWayToSource}" Grid.Row="1" Grid.Column="1"/> 

     <Button Text="Add" Command="{Binding AddCommand}" CommandParameter="{x:Reference EntryName}" 
       Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="0" /> 

    </Grid> 

    <ListView ItemsSource="{Binding Clubs}" 
       Margin="5"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 

         <Label Text="{Binding Path=Name}"/> 
         <Label Text="{Binding Path=Country}" Grid.Column="1"/> 
        </Grid> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

    <StackLayout.BindingContext> 
     <viewModels:ClubListViewModel/> 
    </StackLayout.BindingContext> 

</StackLayout> 

とのViewModelコード:

は、ここに私のXAMLでproperty`sで

private Club _club; 
    public ICommand AddCommand { get; set; } 
    public ICommand RemoveCommand { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public ObservableCollection<Club> Clubs { get; set; } 

    public ClubListViewModel() 
    { 
     AddCommand = new Command(AddClub); 
     RemoveCommand = new Command(RemoveClub); 
     Clubs = new ObservableCollection<Club>(); 
    } 

    public Club Club 
    { 
     get => _club; 
     set 
     { 
      _club = value; 
      OnPropertyChanged("Club"); 
     } 
    } 

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

    //Commands 
    private void AddClub() 
    { 
    } 

ブレークポイントの設定は、クラブのセクションを設定し、異なるモードを試してみたが、それは決して停止しません。

+0

「DataContext」はどこですか? –

+0

問題にはxamlの一部があります。データコンテキストはxamlファイルの最後にあります。 – Max

+0

あなたはその部分を貼り付けることができますか? 'DataContext'が重要なので –

答えて

0

私が変更する必要があるのは、すべて私のViewModelクラスのコンストラクタにClub = new Club();を追加することです。

関連する問題