更新时间:2023-10-04 20:06:16
Itemssource是您正在进行的收藏。
Itemssource is the collection you're feeding in.
应绑定到List< DateTime>或ObservableCollection< DateTime>
That should be bound to a List<DateTime> or ObservableCollection<DateTime>
您可以通过模板化来设置项目的格式。
You can format the items by templating them.
这是一个完整的工作示例。
Here's a complete working example.
Title="MainWindow" Height="350" Width="525"
>
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding CollectionOfDates}"
SelectedItem="{Binding SelectedDate}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ., StringFormat={}{0:dd MMMM yy}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
和一个viewmodel
and a viewmodel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace wpf_99
{
public class MainWindowViewModel : INotifyPropertyChanged
{
private ObservableCollection<DateTime> collectionOfDates
= new ObservableCollection<DateTime>();
public ObservableCollection<DateTime> CollectionOfDates
{
get { return collectionOfDates; }
set { collectionOfDates = value; RaisePropertyChanged(); }
}
private DateTime selectedDate;
public DateTime SelectedDate
{
get { return selectedDate; }
set { selectedDate = value; RaisePropertyChanged();
Console.WriteLine(
" Date Chosen:{value}");
}
}
公共事件PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] String propertyName ="")
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
public MainWindowViewModel()
{
CollectionOfDates = new ObservableCollection< DateTime>
(new List< DateTime> {DateTime.Now,DateTime.Now.Subtract(TimeSpan.FromDays(1))});
}
}
}
"Date Chosen: {value}"); } } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged([CallerMemberName] String propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public MainWindowViewModel() { CollectionOfDates = new ObservableCollection<DateTime> (new List<DateTime> { DateTime.Now, DateTime.Now.Subtract(TimeSpan.FromDays(1)) }); } } }
&NBSP; &NBSP; &NBSP; &NBSP;