在 WPF 应用程序中,我有一个 ListView:
<ListView Height="100" Width="434" Margin="0,2,0,0" x:Name="lvItems" ItemsSource="{Binding ElementName=MainWindow, Path=ShowQuCollection}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Time" Width="100" DisplayMemberBinding="{Binding Time}"/>
<GridViewColumn Header="Description" Width="200" DisplayMemberBinding="{Binding Description}"/>
</GridView>
</ListView.View>
</ListView>
通过数据绑定(bind)与 ObservableCollection 连接。使用 LINQ to SQL 从 SQLServer 数据库表填充 ObservableCollection。
ObservableCollection<ShowsQu> _ShowQuCollection =
new ObservableCollection<ShowsQu>();
public ObservableCollection<ShowsQu> ShowQuCollection
{ get { return _ShowQuCollection; } }
public class ShowsQu
{
public string ShowCode { get; set; }
public DateTime Date { get; set; }
public TimeSpan Time { get; set; }
public string Description { get; set; }
}
private void VisualizeAllShows()
{
MyfirstdbDataContext context = new MyfirstdbDataContext();
_ShowQuCollection.Clear();
var sh = from p in context.Shows select p;
foreach (var p in sh)
_ShowCollection.Add(new ShowsQu
{
Date = p.Date,
Time = p.Time,
Description = p.Description
});
}
问题是 ListView 按原样显示 SQLServer 数据库表中的日期字段 - 没有应用区域特定格式(我猜它应该默认应用)。
我应该在哪里以及如何按照 PC 区域设置的方式对其进行格式化?
最佳答案
出于某种原因,WPF 不会自动获取当前区域性,您必须自己进行设置。一种简单的方法是在您的应用程序启动事件中添加以下代码:
using System.Windows;
using System.Windows.Markup;
using System.Globalization;
...
void App_Startup(object sender, StartupEventArgs e)
{
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
关于c# - WPF ListView 中的 DateTime 区域特定格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1993046/