2016-06-21 21 views
0

Ich habe DatePicker, ListView und Button-Steuerelemente. Nach dem Klicken auf den Button "Hinzufügen", sollte das ausgewählte Datum von DatePicker zu ListView hinzugefügt werden. Modell für Listview ist wie-Hinzufügen des ausgewählten Datums zu Custom ListView in Xamarin Forms

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

ich unten Code verwenden die gewählte Datum in Liste hinzufügen, aber ich bin nicht in der Lage mehr als einen Eintrag in der Liste hinzuzufügen.

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; 

    } 

Ich brauche eine weitere Hilfe für das Element aus Liste zu löschen, nachdem auf Schaltfläche Löschen klicken.

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

Meine XAML-Code -

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

Dies ist, wie mein Listen-

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

Vielen Dank im Voraus.

+0

Bitte fügen Sie mehr Code, selectedDates in dem Modell gibt es das? Verwenden Sie BindingContext? –

+0

Versuchen Sie, 'ObservableCollection' anstelle von' List' zu verwenden. –

+0

** ObservableCollection ** ist komplett neu für mich. Keine Option mit ** List ** – Dipak

Antwort

1

Man könnte so etwas wie dies versuchen:

<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); 
} 
Verwandte Themen