使用SQlite绑定GroupedItems模板但仅获取GroupName
此代码来自datasource类。我正在从SQLite数据库获取客户列表,并将其存储在使用SQlite绑定GroupedItems模板但仅获取GroupName,sqlite,xaml,data-binding,gridview,windows-8,Sqlite,Xaml,Data Binding,Gridview,Windows 8,此代码来自datasource类。我正在从SQLite数据库获取客户列表,并将其存储在ObservableCollection中。使用GetGroups()我正在基于某些属性创建组: public ObservableCollection<CustomerDetails> GetAllCustomers() { using (var con = new SQLiteConnection(app.DBPath)) {
ObservableCollection
中。使用GetGroups()
我正在基于某些属性创建组:
public ObservableCollection<CustomerDetails> GetAllCustomers()
{
using (var con = new SQLiteConnection(app.DBPath))
{
ObservableCollection<CustomerDetails> newCol = new ObservableCollection<CustomerDetails>(con.Query<CustomerDetails>("Select * from CustomerDetails"));
return newCol;
}
}
public IEnumerable<IGrouping<int,CustomerDetails>> GetGroups()
{
return GetAllCustomers().OrderBy(x=>x.CustomerName).GroupBy(x=>x.CustomerPropertyType);
}
XAML文件:
CustomerName
、ContactNo1
和EmailId
是DataSource
中的属性。所有这些都绑定在上面的代码中
<CollectionViewSource
x:Name="groupedItemsViewSource"
Source="{Binding Groups}"
IsSourceGrouped="true"/>
<GridView
x:Name="itemGridView"
IsItemClickEnabled="True"
IsSwipeEnabled="True"
Grid.RowSpan="2"
Padding="116,136,116,46"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource groupedItemsViewSource}}"
SelectionMode="Single"
SelectedItem="0">
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="320" Height="240">
<StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding CustomerName}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="48" Margin="15,0,15,0"/>
<TextBlock Text="{Binding ContactNo1}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="48" Margin="15,0,15,0"/>
<TextBlock Text="{Binding EmailId}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="48" Margin="15,0,15,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Style="{StaticResource TextPrimaryButtonStyle}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
我认为SQLite net是延迟实现的,因此在您尝试访问集合中的项之前,查询实际上不会给出任何结果。尝试将ToList()放在查询调用的末尾:
public ObservableCollection<CustomerDetails> GetAllCustomers()
{
using (var con = new SQLiteConnection(app.DBPath))
{
// add ToList() to query to instantiate the results
ObservableCollection<CustomerDetails> newCol = new ObservableCollection<CustomerDetails>(con.Query<CustomerDetails>("Select * from CustomerDetails").ToList());
return newCol;
}
}
publicobservableCollection GetAllCustomers()
{
使用(var con=newsqliteconnection(app.DBPath))
{
//将ToList()添加到查询以实例化结果
ObservableCollection newCol=新的ObservableCollection(con.Query(“从CustomerDetails中选择*).ToList());
返回newCol;
}
}
我重新创建了您的解决方案,并在DefaultViewModel
中找到了问题。使用您自己的DefaultViewModel实现,或者称之为MainViewModel,它实现了INotifyPropertyChanged
,例如:
public class MainViewModel : INotifyPropertyChanged
{
private IEnumerable<IGrouping<int, CustomerDetails>> groups = null;
public IEnumerable<IGrouping<int, CustomerDetails>> Groups
{
get { return groups; }
private set { Set(ref groups, value); }
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private bool Set<T>(ref T storage, object value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(storage, value))
return false;
storage = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
return true;
}
#endregion
}
public类主视图模型:INotifyPropertyChanged
{
私有IEnumerable组=null;
公共可数群
{
获取{返回组;}
私有集{set(ref组,值);}
}
#区域INotifyPropertyChanged实现
公共事件属性更改事件处理程序属性更改;
私有布尔集合(参考T存储,对象值,[CallerMemberName]字符串propertyName=null)
{
if(object.Equals(存储,值))
返回false;
储存=价值;
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
返回true;
}
#端区
}
然后将页面的DataContext设置为MainViewModel的实例,并使用所需的数据设置Groups属性(也应该在MainViewModel中,例如,使用一些
LoadGroups
方法)。页面资源中的CollectionViewSource引用了MainViewModel的Groups属性,您将在GridView中看到您的数据。我检查了查询是否返回CustomerDetails表的所有属性……我也尝试了您的方法,但结果相同。
public class MainViewModel : INotifyPropertyChanged
{
private IEnumerable<IGrouping<int, CustomerDetails>> groups = null;
public IEnumerable<IGrouping<int, CustomerDetails>> Groups
{
get { return groups; }
private set { Set(ref groups, value); }
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private bool Set<T>(ref T storage, object value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(storage, value))
return false;
storage = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
return true;
}
#endregion
}