格式列表框日期 [英] Format Listbox Dates
本文介绍了格式列表框日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我无法弄清楚如何格式化列表框日期。我使用了与我的数据网格相同的方法,但它不起作用。
I can't figure out how to format my listbox dates. I used the same method that worked for my datagrid but it isn't working.
<ListBox x:Name="list" ItemsSource="{Binding Date, StringFormat='MMMM'}" DisplayMemberPath="Date" HorizontalAlignment="Left" Height="343" Margin="647,46,0,0" VerticalAlignment="Top" Width="100" SelectionChanged="list_SelectionChanged"/>
我的数据库中的默认日期格式没有变化。
The defaulted date format from my database isn't changing.
谢谢,
- Justair07
- Justair07
推荐答案
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;
这篇关于格式列表框日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文