在我的WP8.1(WinRT)应用程序中,我有一个listview控件,其中的分组定义了数据绑定。但是当我运行应用程序时,我没有看到任何数据项或任何绑定错误。
我无法弄清楚错误是什么。这是我的XAML和代码,以及如何将其扩展到跳转列表?
我的xaml
<DataTemplate x:Key="DataTemplate1">
<StackPanel VerticalAlignment="Top">
<TextBlock Text="{Binding Name}" FontSize="26" Margin="12,-12,12,6"/>
<TextBlock Text="{Binding Country}" Foreground="GreenYellow"/>
<TextBlock Text="{Binding Language}" Foreground="Orange" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="GroupHeader">
<Border Background="Transparent">
<Border Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
Width="400" Height="90" HorizontalAlignment="Left">
<TextBlock Text="{Binding Key}"
Foreground="{StaticResource PhoneAccentBrush}"
FontSize="28"
Padding="2"
FontFamily="{StaticResource PhoneFontFamilySemiLight}"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
</Border>
</Border>
<Grid>
<Grid.DataContext>
<testApp:DemoViewModel/>
</Grid.DataContext>
<ListView x:Name="CitiesList" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding CityByCountry}">
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource GroupHeader}" />
</ListView.GroupStyle>
</ListView>
</Grid>
和我的c#(Model,ViewModel)
public class DemoViewModel
{
public List<KeyedList<string, City>> CityByCountry { get; set; }
}
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key { protected set; get; }
public KeyedList(TKey key, IEnumerable<TItem> items): base(items)
{
Key = key;
}
public KeyedList(IGrouping<TKey, TItem> grouping): base(grouping)
{
Key = grouping.Key;
}
}
public class City
{
public string Name { get; set; }
public string Country { get; set; }
public string Language { get; set; }
}
我使用下面的代码加载demodata以便在主页面中进行测试
List<City> source = new List<City>
{
new City {Name = "Madrid", Country = "ES", Language = "Spanish"},
new City {Name = "Barcelona", Country = "ES", Language = "Spanish"},
new City {Name = "Mallorca", Country = "ES", Language = "Spanish"},
new City {Name = "Las Vegas", Country = "US", Language = "English"},
new City {Name = "Dalas", Country = "US", Language = "English"},
new City {Name = "New York", Country = "US", Language = "English"},
new City {Name = "London", Country = "UK", Language = "English"},
new City {Name = "Mexico", Country = "MX", Language = "Spanish"},
new City {Name = "Milan", Country = "IT", Language = "Italian"},
new City {Name = "Roma", Country = "IT", Language = "Italian"},
new City {Name = "Paris", Country = "FR", Language = "French"}
};
// this.citiesList.ItemsSource = source;
DemoViewModel viewModel = new DemoViewModel();
var tmp = from city in source
group city by city.Country into c
orderby c.Key
select new KeyedList<string, City>(c.Key, c);
viewModel.CityByCountry = new List<KeyedList<string, City>>(tmp);
DataContext = viewModel;
答案 0 :(得分:2)
您必须使用CollectionViewSource进行分组。希望这可以帮助你Jumplists in Windows Phone 8.1 (Updated)
并将您的tmp设置为Source