将所选日期添加到Xamarin Forms中的Custom ListView

时间:2016-06-21 07:14:41

标签: listview datepicker xamarin.forms

我有DatePicker,ListView和Button控件。 单击按钮"添加"后,应将DatePicker的选定日期添加到ListView。 ListView的模型就像 -

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

我使用下面的代码在List中添加所选日期,但我无法在列表中添加多个条目。

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>{ };

提前致谢。

1 个答案:

答案 0 :(得分: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);
}