0

此代码来自数据源类。我正在从 SQLite 数据库中获取客户列表并将其存储在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);
        }

这就是我绑定网格视图的方式

        CustomerImplementation objCustomerImp = new CustomerImplementation();
        var all = objCustomerImp.GetGroups();

        this.DefaultViewModel["Groups"] = all;

XAML 文件:

CustomerName,ContactNo1EmailId是里面的属性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>
4

2 回答 2

0

我重新创建了您的解决方案,并在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
}

然后将您的 Page 的 DataContext 设置为 MainViewModel 的一个实例,并使用您想要的数据设置 Groups 属性(也应该在 MainViewModel 中,例如,使用某种LoadGroups方法)。页面资源中的 CollectionViewSource 引用 MainViewModel 的 Groups 属性,您将在 GridView 中看到您的数据。

于 2014-12-05T08:35:11.207 回答
0

我相信 SQLite-net 是延迟实现的,因此在您尝试访问集合中的项目之前,查询实际上不会给出任何结果。尝试将 ToList() 放在 Query 调用的末尾:

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;
    }
}
于 2012-12-24T21:33:43.960 回答