2016-06-21 14 views
0

DatePicker、ListView、およびButtonコントロールがあります。 「追加」ボタンをクリックした後、DatePickerの選択された日付をListViewに追加する必要があります。 ListViewコントロールのための モデルは、私がリストで選択した日付を追加するためのコードの下に使用していますが、私は、リスト内の複数のエントリを追加することはできませんよXamarinフォームのカスタムリストビューに選択した日付を追加します。

public class dateListModel 
    { 
     public string dateSelected { get; set; } 
     public string requestFor { get; set; } 
     public int id { get; set; } 
     public string weekDay { get; set; } 

    } 

enter image description here

like-です。

public void onAddClicked(object sender, EventArgs e) 
    {  

     selectedDates.Add (new dateListModel(){dateSelected=choosedDate.Date.ToString("d"),requestFor=requestFor.Items[requestFor.SelectedIndex],id=1,weekDay=choosedDate.Date.DayOfWeek.ToString()}); 
     listview_MenuItem.ItemsSource=selectedDates; 

    } 

[削除]ボタンをクリックした後、リストからそのアイテムを削除するには、もう一度ヘルプが必要です。

public void onDeleteClicked(object sender, EventArgs e) 
     { 
      //How delete particular item from list 
     } 

私のXAMLコード -

<StackLayout Spacing="20" Padding="20"> 
     <StackLayout Orientation="Horizontal"> 
      <Label Text="I need to apply for a " TextColor="{x:Static color:ColorResources.TextColor}"/> 
      <Picker x:Name="requestFor" WidthRequest="150">    
      </Picker> 
     </StackLayout> 

     <StackLayout Orientation="Horizontal"> 
      <Label Text="Choose date:" VerticalOptions="CenterAndExpand" TextColor="{x:Static color:ColorResources.TextColor}" /> 
      <DatePicker x:Name="choosedDate" WidthRequest="150" HorizontalOptions="EndAndExpand" Date="{x:Static sys:DateTime.Now}"> 
       <DatePicker.Format> dd-MMM-yyyy</DatePicker.Format> 
      </DatePicker> 
      <Button Clicked="onAddClicked" HorizontalOptions="EndAndExpand" TextColor="White" HeightRequest="35" WidthRequest="70" Text=" Add " BackgroundColor="#ed1f29"/> 
     </StackLayout> 

     <ListView x:Name ="listview_MenuItem" BackgroundColor="#ffffcc" RowHeight="75" > 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
        <StackLayout Orientation="Vertical"> 
        <Grid HeightRequest="0.5" BackgroundColor="Red"/> 
         <StackLayout Orientation="Horizontal" Spacing="30"> 
         <StackLayout Orientation="Vertical"> 
          <Label Text="{Binding dateSelected}" TextColor="{x:Static color:ColorResources.TextColor}" VerticalOptions="CenterAndExpand"/> 
          <StackLayout Orientation="Horizontal"> 
           <Label Text="{Binding requestFor}" 
            TextColor="{x:Static color:ColorResources.commonButtonBackgroundColor}"/><Label Text=" - "/> 
           <Label Text="{Binding weekDay}" TextColor="{x:Static color:ColorResources.TextColor}" 
            HorizontalOptions="StartAndExpand" /> 
          </StackLayout> 
         </StackLayout> 
          <Button WidthRequest="70" HeightRequest="30" TextColor="{x:Static color:ColorResources.btnTextColor}" BackgroundColor="#ed1f29" Clicked="onDeleteClicked" Text="Delete" FontSize="12" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/> 
<!--       <Image Source="delete.png" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="40" WidthRequest="45"/>        --> 
          </StackLayout> 
        </StackLayout> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

これは、事前にどのように私のリスト -

List<dateListModel> selectedDates = new List<dateListModel>{ }; 

おかげです。あなたはこのような何かを試みることができる

+0

より多くのコードを追加してくださいし、selectedDatesはどのモデルでは、これは存在するのでしょうか? BindingContextを使用していますか? –

+0

'List'の代わりに' ObservableCollection'を使うようにしてください。 –

+0

** ObservableCollection **は私にとって全く新しいものです。 ** List ** – Dipak

答えて

1

<ListView x:Name ="listview_MenuItem" 
     BackgroundColor="#ffffcc" 
     RowHeight="75"> 
<ListView.ItemTemplate> 
    <DataTemplate> 
    <ViewCell> 
     <StackLayout Orientation="Vertical"> 
     <Grid HeightRequest="0.5" BackgroundColor="Red"/> 
     <StackLayout Orientation="Horizontal" Spacing="30"> 
      <Button Clicked="onDeleteClicked" 
        BindingContext="{Binding}"/> 
     </StackLayout> 
     </StackLayout> 
    </ViewCell> 
    </DataTemplate> 
</ListView.ItemTemplate> 
</ListView> 


public void onDeleteClicked(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    var itemForRemoving = button.BindingContext as dateListModel; 
    selectedDates.Remove(itemForRemoving); 
} 
関連する問題