C# 从数组加载带有日期时间值的ObservableCollection
我在一个名为HistoryStatistics的类中有一个ListView,其模板如下所示。每个项目都有一个按钮和两个文本块C# 从数组加载带有日期时间值的ObservableCollection,c#,listview,data-binding,windows-8,C#,Listview,Data Binding,Windows 8,我在一个名为HistoryStatistics的类中有一个ListView,其模板如下所示。每个项目都有一个按钮和两个文本块 <ListView x:Name="HistoryLV" Grid.Row="1" Grid.Column="1" Margin="20,20,20,20"> <ListView.ItemTemplate> <DataTemplate> <StackP
<ListView x:Name="HistoryLV" Grid.Row="1" Grid.Column="1" Margin="20,20,20,20">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button/>
<TextBlock x:Name="historyDatesTBlock" Text="{Binding}" Foreground="Coral"/>
<TextBlock x:Name="cycleLengthTBlock" Foreground="AliceBlue"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
现在我需要用两个不同的值绑定文本框。我有一门课叫CycleManager。它有一个日期时间数组。我想将数组的前n个值数据绑定到第一个textblock
在HistoryStatistics的构造函数中,我有一个名为LoadListView的函数
public void LoadListView()
{
CycleManager cycMan = CycleManager.Instance;
DateTime[] items = cycMan.GetHistoryArray();
HistoryList = new ObservableCollection<DateTime>(items);
HistoryLV.DataContext = HistoryList;
}
public void LoadListView()
{
CycleManager cycMan=CycleManager.Instance;
DateTime[]items=cycMan.GetHistoryArray();
HistoryList=新观察到的集合(项目);
HistoryLV.DataContext=HistoryList;
}
这是绑定DateTime的正确方法吗?我尝试添加到字符串,但没有显示日期
是否有一种方法可以在代码隐藏中创建ListView,并通过forloop将各个项目与我想要的值绑定?请尝试将以下行添加到LoadListView方法中。我自己没有尝试过,因为我的系统上没有安装Windows 8
HistoryLV.ItemsSource = HistoryList;
尝试:
CycleManager cycMan=CycleManager.Instance;
DateTime[]items=cycMan.GetHistoryArray();
string[]list=新字符串[items.Length];
对于(int i=0;ii正在为Windows 8oh ya:开发),您仍然可以尝试设置数据源您可能需要在HistoryLV
中添加ItemsSource=“{Binding}”
。
CycleManager cycMan = CycleManager.Instance;
DateTime[] items = cycMan.GetHistoryArray();
string[] list = new string[items.Length];
for(int i=0; i<items.Length; i++)
{
list[i] = items[i].ToString("**The DateTime format you want**");
}
HistoryList = new ObservableCollection<string>(list);
HistoryLV.ItemsSource = HistoryList;