rem*_*rem 2 c# data-binding wpf listview linq-to-sql
在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>
Run Code Online (Sandbox Code Playgroud)
通过数据绑定与ObservableCollection连接.使用LINQ to SQL从SQLServer db表填充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
});
}
Run Code Online (Sandbox Code Playgroud)
问题是ListView按原样显示SQLServer数据库表中的Date字段 - 没有应用特定于区域的格式(我猜测它应该默认应用).
我应该在哪里以及如何将其格式化为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)));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1991 次 |
| 最近记录: |