本文承接“為 ListView 和 GridView 添加數(shù)據(jù)”。
在上一節(jié)中我們已經(jīng)了解了怎樣將數(shù)據(jù)綁定到 ListView 或 GridView,但既然要用到這兩個(gè)控件往往是因?yàn)閿?shù)據(jù)繁多,那么幾乎就不可避免的要讓其能夠分組。我們所綁定的數(shù)據(jù)源可能是項(xiàng)列表,其中的每個(gè)項(xiàng)甚至還有其自己的項(xiàng),那么問(wèn)題就來(lái)了。
一時(shí)不會(huì)也想不出什么宏偉的例子,就做一個(gè)簡(jiǎn)單的鬧鐘的時(shí)間表的 ListView 和 GridView 吧。那么先在項(xiàng)目中添加一個(gè)類,最好在 Shared 下。內(nèi)容都是很簡(jiǎn)易的,鬧鐘的標(biāo)題、時(shí)間、備注等,為了增加一級(jí)目錄就加了一個(gè) AlarmMode,就算作學(xué)習(xí)和生活吧,學(xué)習(xí)生活兩不誤……
public class Alarm
{
public string Title { get; set; }
public DateTime AlarmClockTime { get; set; }
public string Description { get; set; }
public string AlarmMode { get; set; }
}
public class AlarmMode
{
public AlarmMode()
{
alarmMode = new ObservableCollection<Alarm>();
}
public string Name { get; set; }
public ObservableCollection<Alarm> alarmMode { get; private set; }
}
首先,先來(lái)定義一個(gè)全局的時(shí)間,然后在頁(yè)面加載時(shí)加載兩個(gè)函數(shù)(將在下一步定義)。
DateTime globalTime;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DateTime.TryParse("1/1/2115", out globalTime);
AddAlarm();
AddAlarmMode();
}
一大波數(shù)據(jù)正在靠近!
private void AddAlarm()
{
List<Alarm> listAlarm = new List<Alarm>();
listAlarm.Add(new Alarm()
{
Title = "Alarm1",
Description = "First Alarm",
AlarmClockTime = globalTime.AddHours(1),
AlarmMode = "Alarm In Life"
});
listAlarm.Add(new Alarm()
{
Title = "Alarm2",
Description = "Second Alarm",
AlarmClockTime = globalTime.AddHours(11),
AlarmMode = "Alarm In Life"
});
listAlarm.Add(new Alarm()
{
Title = "Alarm3",
Description = "Third Alarm",
AlarmClockTime = globalTime.AddDays(1),
AlarmMode = "Alarm In Life"
});
listAlarm.Add(new Alarm()
{
Title = "Alarm1",
Description = "First Alarm",
AlarmClockTime = globalTime.AddHours(12),
AlarmMode = "Alarm In Study"
});
listAlarm.Add(new Alarm()
{
Title = "Alarm2",
Description = "Second Alarm",
AlarmClockTime = globalTime.AddHours(15),
AlarmMode = "Alarm In Study"
});
listAlarm.Add(new Alarm()
{
Title = "Alarm3",
Description = "Third Alarm",
AlarmClockTime = globalTime.AddMonths(1),
AlarmMode = "Alarm In Study"
});
ar alarmSetting = from ala in listAlarm
group ala
by ala.AlarmMode
into alaSetting
orderby alaSetting.Key
select alaSetting;
collectionVSAlarm.Source = alarmSetting;
}
private void AddAlarmMode()
{
List<AlarmMode> listAlarmMode = new List<AlarmMode>();
AlarmMode am1 = new AlarmMode();
am1.Name = "Alarm In Life";
am1.alarmMode.Add(new Alarm()
{
Title = "Alarm1",
Description = "First Alarm",
AlarmClockTime = globalTime.AddHours(1),
});
am1.alarmMode.Add(new Alarm()
{
Title = "Alarm2",
Description = "Second Alarm",
AlarmClockTime = globalTime.AddHours(11),
});
am1.alarmMode.Add(new Alarm()
{
Title = "Alarm3",
Description = "Third Alarm",
AlarmClockTime = globalTime.AddDays(1),
});
listAlarmMode.Add(am1);
AlarmMode am2 = new AlarmMode();
am2.Name = "Alarm In Study";
am2.alarmMode.Add(new Alarm()
{
Title = "Alarm1",
Description = "First Alarm",
AlarmClockTime = globalTime.AddHours(12),
});
am2.alarmMode.Add(new Alarm()
{
Title = "Alarm2",
Description = "Second Alarm",
AlarmClockTime = globalTime.AddHours(15),
});
am2.alarmMode.Add(new Alarm()
{
Title = "Alarm3",
Description = "Third Alarm",
AlarmClockTime = globalTime.AddMonths(1),
});
listAlarmMode.Add(am2);
collectionVSAlarmMode.Source = listAlarmMode;
}
這些數(shù)據(jù)都是亂七八糟啦,大家湊合著看。這是兩個(gè)函數(shù),數(shù)據(jù)我都是用 List<>
來(lái)定義的,將數(shù)據(jù)通過(guò) Add 函數(shù)添加到 listAlarm 和 listAlarmMode 中即可。最后再?gòu)?listAlarm 中根據(jù) AlarmMode 挑出數(shù)據(jù)到 alaSetting,同時(shí)還要根據(jù) Key 值進(jìn)行排序最后選出并連接到collectionVSAlarm的 Source 屬性中。這個(gè)是需要在 MainPage.xaml 中定義的哦,就像 <Page.Resource/>
一樣。還需要 ItemsPath 屬性來(lái)定義其的子對(duì)象喲。
<UserControl.Resources>
<CollectionViewSource x:Name="collectionVSAlarm" IsSourceGrouped="True"/>
<CollectionViewSource x:Name="collectionVSAlarmMode" IsSourceGrouped="True" ItemsPath="alarmMode"/>
</UserControl.Resources>
然后我們還需要?jiǎng)?chuàng)建一個(gè) ListGridGroupStyle 類來(lái)繼承 GroupStyleSelector,重載它的 SelectGroupStyleCore 方法,并且返回 ListGridGroupStyleResource 資源,這個(gè)資源在博客后文中有定義,其定義在 App.xaml 中。相應(yīng)的代碼如下咯:
public class ListGridGroupStyle : GroupStyleSelector
{
protected override GroupStyle SelectGroupStyleCore(object group, uint level)
{
return (GroupStyle)App.Current.Resources["ListGridGroupStyleResource"];
}
}
方法重載好之后就需要在前面的 UserControl.Resources 中加上以下這條代碼啦。
<local:ListGridGroupStyle x:Key="ListGridGroupStyleResource"/>
然后我們來(lái)一系列的基本樣式到 App.xaml 中就好啦,關(guān)于資源文件的使用我們?cè)诤竺鏁?huì)系統(tǒng)的來(lái)學(xué)習(xí)。這里的 DataTemplate 和 GroupStyle 都在資源字典中,前者是 Template 模板,后者是 Style 風(fēng)格。內(nèi)容的排版大家都隨意啦,記得設(shè)置好 Key 值。
<Application.Resources>
<ResourceDictionary>
<DataTemplate x:Key="dataTemplateListView">
<StackPanel Width="700" Margin="10">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" FontWeight="Bold" Margin="12"/>
<TextBlock Text="{Binding AlarmClockTime}" TextWrapping="NoWrap" Margin="12"/>
<TextBlock Text="{Binding Description}" TextWrapping="NoWrap" Margin="12"/>
</StackPanel>
</StackPanel>
</DataTemplate>
<GroupStyle x:Key="ListGridGroupStyleResource">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Background="LightGray" >
<TextBlock Text='{Binding Key}' Foreground="CornflowerBlue" Margin="12" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ResourceDictionary>
</Application.Resources>
那么這些各種資源都定義好了之后就在 MainPage.xaml 把下面這些敲進(jìn)去。各種資源的調(diào)用在這里尤其需要注意,其實(shí)對(duì)于稍微復(fù)雜一丁點(diǎn)的程序而言,名稱就已經(jīng)變得讓人崩潰了。所以擁有一個(gè)良好的命名習(xí)慣很重要。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<GridView Grid.Column="0" ItemsSource="{Binding Source={StaticResource collectionVSAlarmMode}}"
Margin="12,120,12,12" MaxHeight="600" >
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="18">
<TextBlock Text="{Binding Title}" FontWeight="ExtraBold" />
<TextBlock Text="{Binding AlarmClockTime}" FontWeight="Light" TextWrapping="NoWrap" />
<TextBlock Text="{Binding Description}" TextWrapping="NoWrap" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="2"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Background="Green" Margin="12">
<TextBlock Text='{Binding Name}'
Foreground="Bisque" Margin="36"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
<ListView Grid.Column="1" ItemsSource="{Binding Source={StaticResource collectionVSAlarm}}"
ItemTemplate="{StaticResource dataTemplateListView}"
GroupStyleSelector="{StaticResource ListGridGroupStyleResource}"
Margin="120" />
</Grid>
我這寫(xiě)的真是太丑了哎,做產(chǎn)品的時(shí)候可得好好調(diào)調(diào)了。
更多建議: