为什么gridview会像它具有数据一样展开但不显示数据?

时间:2019-03-11 01:02:02

标签: c# xaml uwp windows-10-universal

数据很好,我可以在列表框中成功显示数据,但是当我尝试在网格视图或列表视图中执行此操作时,我得到了这些结果。我觉得这里缺少一些小东西。

我已经设置了两种方式,并且在文本框中更新了属性源,但是仍然没有显示可见数据。我知道它具有数据,因为它像I captured in this screenshot一样扩展。

LeadingCauseOfDeath类

public class LeadingCausesOfDeath : BasePropertyHandler
{
    public int Year { get; set; }
    public string _113_cause_name { get;set;}
    public string Cause_name { get; set; }
    public string State { get; set; }
    public int Deaths { get; set; }
    public int AgeAdjustedDeathRate { get; set; }

}

XAML视图

<Grid DataContext="{Binding Source={StaticResource HomePageVM}}">
    <StackPanel >
        <NavigationView PaneDisplayMode="Top" IsBackButtonVisible="Collapsed" >
            <NavigationView.MenuItems>
                <NavigationViewItem Content="Family"/>
                <NavigationViewItem Content="History"/>
                <NavigationViewItem Content="About"/>
            </NavigationView.MenuItems>
        </NavigationView>

        <GridView ItemsSource="{Binding Data, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  IsItemClickEnabled="True"
              IsSwipeEnabled="true"
              SelectionMode="Single"
                  BorderThickness="10" BorderBrush="Blue">

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid MaximumRowsOrColumns="4" Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>

            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid  BorderBrush="Black" BorderThickness="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding Cause_name}" />
                        <TextBlock Grid.Column="1" Text="{Binding Year}" />
                        <TextBlock Grid.Column="2" Text="{Binding Deaths}" />
                        <TextBlock Grid.Column="3" Text="{Binding State}" />
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>

        </GridView>
    </StackPanel>
</Grid>

查看模型

 private ObservableCollection<LeadingCausesOfDeath> _data;

    public ObservableCollection<LeadingCausesOfDeath> Data
    {
        get
        {
            return _data;
        }
        set
        {
            _data = value;
            OnPropertyChanged("Data");
        }
    }
    public HomePageVM()
    {
        GetData();
    }

    private async void GetData()
    {
        var service = new ServiceForApi();
        Data = new ObservableCollection<LeadingCausesOfDeath>();
        Data = await service.GetFDAStuff();
        OnPropertyChanged("Data");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged( string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

0 个答案:

没有答案