【问题标题】:Using SQlite to bind GroupedItems Template but only getting GroupNames使用 SQlite 绑定 GroupedItems 模板但只获取 GroupNames
【发布时间】:2012-12-11 02:59:24
【问题描述】:

此代码来自数据源类。我正在从 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 文件:

CustomerNameContactNo1EmailIdDataSource 中的属性。都绑定在上面的代码中。

<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 xaml data-binding gridview windows-8


    【解决方案1】:

    我相信 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;
        }
    }
    

    【讨论】:

    • 我已经检查了查询是否返回了 CustomerDetails 表的所有属性......我也用你的方式尝试过,但结果是一样的。
    【解决方案2】:

    我重新创建了您的解决方案,并在 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 中看到您的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2011-07-20
      相关资源
      最近更新 更多