C# WPF ListView中的日期时间区域特定格式
在WPF应用程序中,我有一个列表视图:C# WPF ListView中的日期时间区域特定格式,c#,wpf,linq-to-sql,data-binding,listview,C#,Wpf,Linq To Sql,Data Binding,Listview,在WPF应用程序中,我有一个列表视图: <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"
<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>
它通过数据绑定与ObservableCollection连接。ObservableCollection是使用LINQ to SQL从SQLServer db表填充的
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
});
}
ObservableCollection\u showqCollection=
新的可观察集合();
公共可观测集合ShowQuCollection
{get{return\u ShowQuCollection;}}
公开课
{
公共字符串ShowCode{get;set;}
公共日期时间日期{get;set;}
公共时间跨度时间{get;set;}
公共字符串说明{get;set;}
}
private-void-allshows()
{
MyfirstdbDataContext=新的MyfirstdbDataContext();
_ShowQuCollection.Clear();
var sh=上下文中的p。显示选择p;
foreach(上海的var p)
_ShowCollection.Add(新的ShowsQu)
{
日期=p.日期,
时间=p.时间,
描述=p.描述
});
}
问题是ListView按原样显示SQLServer数据库表中的日期字段,而不应用特定于区域的格式(我猜默认情况下应该应用)
我应该在何处以及如何将其格式化为PC区域设置的格式?来自MSDN:
You can pass a CultureInfo object representing the culture whose formatting is to be used to a method that has an IFormatProvider parameter. The following example displays a date using the short date format of the pt-BR culture.
从这里开始:
这里有更多信息:
希望这有帮助。由于某些原因,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)));
}
我转到了其他几个搜索结果,因为我觉得这不是我的解决方案,因为日期格式错误的唯一地方是在ListView/GridViewColumn中。然而,这就是解决方案。请有人解释一下。DatePicker,DateTimePicker,都在我的文化中工作,没有这个,但没有ListView。谢谢