I hate having to look deep and hard and then when I don’t find anything on Google, I move into uncharted territory. So here is some code on how to bind a Silverlight Listview with LINQ.
One point, I don’t care that much about making this that presentable, so here is what I wrote. The LINQ could probably be built better, but if you know what I work or who I work for, you know that all I build is Demo Apps. So this won’t be production, its just showing the fact that it can be done. This Demo shows Hurricanes in a Listview. The list of hurricanes were pulled from the NOAA site.
A few things to point out:
- I pulled all the hurricanes from the DB and then started working on them. So everything is already in cache.
- When doing Hierarchical data tempates, the ITEMSOURCE and ITEM templates are point to the the next lower level of the Template. So if you look in the XAML, you will see the the item source and item templates point to the next layer down. I didn’t know this.
C# Service:
public class HurricaneServices : IHurricaneServices
{
public List<HurricanesClass> GetHurricanes()
{
var hurricanes = HurricanesClass.GetCachedHurricanes();
return(from xx in hurricanes
group xx by xx.Hurricane_Name into gg
orderby gg.FirstOrDefault().DateTime_Reported descending
where gg.Key != null
select new HurricanesClass
{
Hurricane_Name = gg.Key.Replace(".mxd", ""),
Types = (from yy in hurricanes
where yy.Hurricane_Name == gg.Key
group yy by yy.Type into zz
where zz.Key != null
select new HurricanesClass
{
Type = zz.Key,
Services = (from cc in hurricanes
where cc.Type == zz.Key
where cc.Hurricane_Name == gg.Key
orderby cc.DateTime_Reported descending
select new HurricanesClass
{
DateTime_Reported = cc.DateTime_Reported,
Webservice_Name = cc.Webservice_Name,
Webservice_URL = cc.Webservice_URL,
}).ToList()
}).ToList()
}).ToList();
}
}
Here is the XAML it is binded to:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<sdk:HierarchicalDataTemplate x:Key="HurricaneServices">
<StackPanel MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
<TextBlock Text="{Binding Path=Webservice_Name}"></TextBlock>
<TextBlock Text="{Binding Path=DateTime_Reported}"></TextBlock>
<TextBlock x:Name="wsURL" Text="{Binding Path=Webservice_URL}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="HurricaneTypes" ItemsSource="{Binding Path=Services}" ItemTemplate="{StaticResource HurricaneServices}">
<StackPanel>
<TextBlock Text="{Binding Path=Type}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="HurricaneNames" ItemsSource="{Binding Path=Types}" ItemTemplate="{StaticResource HurricaneTypes}">
<StackPanel>
<TextBlock Text="{Binding Path=Hurricane_Name}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
<sdk:TreeView x:Name="mytree" ItemTemplate="{StaticResource HurricaneNames}" Grid.Row="0" Grid.Column="0" Height="300" Width="800" Background="Gray">
</sdk:TreeView>
</Grid>
And last but not least, the Binding Syntax. The code behind of the XAML page.
public HurricaneData()
{
InitializeComponent();
HurricaneService.HurricaneServicesClient ws = new HurricaneService.HurricaneServicesClient();
ws.GetHurricanesCompleted += new EventHandler<HurricaneService.GetHurricanesCompletedEventArgs>(webService_GetCustomersByLastNameCompleted);
ws.GetHurricanesAsync();
}
void webService_GetCustomersByLastNameCompleted(object sender, HurricaneService.GetHurricanesCompletedEventArgs e)
{
mytree.ItemsSource = e.Result;
}
If you liked this post, please be sure to subscribe to my
RSS Feed.