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 ファイル:

CustomerNameContactNo1および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>
4

2 に答える 2

0

SQLite-netは遅延実装されていると思います。そのため、コレクション内のアイテムにアクセスしようとするまで、クエリは実際には結果を返しません。Query呼び出しの最後に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;
    }
}
于 2012-12-24T21:33:43.960 に答える
0

I recreated your solution and found the problem in DefaultViewModel. Use your own implementation of DefaultViewModel, or call it MainViewModel, which implements INotifyPropertyChanged, e.g.:

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
}

Then set the DataContext of your Page to an instance of MainViewModel, and set the Groups property with data you want (should be in MainViewModel too, e.g., with some LoadGroups method). CollectionViewSource in page resources references to Groups property of your MainViewModel and you will see your data in the GridView.

于 2014-12-05T08:35:11.207 に答える