【发布时间】:2016-06-21 18:22:14
【问题描述】:
我有 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; }
}
我正在使用下面的代码在列表中添加选定的日期,但我无法在列表中添加多个条目。
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>{ };
提前致谢。
【问题讨论】:
-
请添加更多代码,selectedDates在哪个模型中存在?你使用 BindingContext 吗?
-
尝试使用
ObservableCollection而不是List。 -
ObservableCollection 对我来说是全新的。 List 没有任何选项
-
像
List一样使用它,但ObservableCollection会通知 UI,一些项目被添加或删除。 -
嗨@EgorGromadskiy 它对我有用。谢谢。我使用 - ObservableCollection
model = new ObservableCollection 而不是 List(); selectedDates = new List { };
标签: listview datepicker xamarin.forms