You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StackLayout中加载Xamarin.Forms.DataGrid仅显示表头无数据求助

Xamarin.Forms.DataGrid仅显示表头无法展示数据求助

在个人Xamarin项目中使用Xamarin.Forms.DataGrid时,将其放入包含多个组件的StackLayout后,发现DataGrid仅能显示表头,无法加载展示数据。尝试给StackLayout添加高度值与VerticalOptions属性后,问题仍未解决,相关代码如下:

初始代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
             x:Class="Spray.Views.QC2">
    <ContentView>
        <StackLayout>
            <Entry x:Name="entryName" Placeholder="Name" Text="lai"/>
            <dg:DataGrid x:Name="dataGridView1"  ItemsSource="{Binding Jobs}" SelectionEnabled="True" RowHeight="70" HeaderHeight="50"
                     BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" PullToRefreshCommand="{Binding RefreshCommand}" 
                     IsRefreshing="{Binding IsRefreshing}" ActiveRowColor="#8899AA" ItemSelected = "onItemSelected">
                <dg:DataGrid.Columns>
                    <dg:DataGridColumn Title="PN" PropertyName="pn" Width="2*"/>
                    <dg:DataGridColumn Title="Quantity" PropertyName="quantity" Width="1*"/>
                    <dg:DataGridColumn Title="Worker" PropertyName="worker" Width="1*"/>
                    <dg:DataGridColumn Title="QC Sign" PropertyName="signature" Width="1*"/>
                    <dg:DataGridColumn Title="Time" PropertyName="time" Width="1*"/>
                </dg:DataGrid.Columns>

                <dg:DataGrid.RowsBackgroundColorPalette>
                    <dg:PaletteCollection>
                        <Color>#F2F2F2</Color>
                        <Color>#FFFFFF</Color>
                    </dg:PaletteCollection>
                </dg:DataGrid.RowsBackgroundColorPalette>
            </dg:DataGrid>
        </StackLayout>
    </ContentView>
</ContentPage>

修改后的代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
             x:Class="Spray.Views.QC2">
    <ContentView>
        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="300">
            <Entry x:Name="entryName" Placeholder="Name" Text="lai"/>
            
            <dg:DataGrid x:Name="dataGridView1"  ItemsSource="{Binding Jobs}" SelectionEnabled="True" RowHeight="70" HeaderHeight="50"
                    BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" PullToRefreshCommand="{Binding RefreshCommand}" 
                    IsRefreshing="{Binding IsRefreshing}" ActiveRowColor="#8899AA" ItemSelected = "onItemSelected">
                <dg:DataGrid.Columns>
                    <dg:DataGridColumn Title="PN" PropertyName="pn" Width="2*"/>
                    <dg:DataGridColumn Title="Quantity" PropertyName="quantity" Width="1*"/>
                    <dg:DataGridColumn Title="Worker" PropertyName="worker" Width="1*"/>
                    <dg:DataGridColumn Title="QC Sign" PropertyName="signature" Width="1*"/>
                    <dg:DataGridColumn Title="Time" PropertyName="time" Width="1*"/>
                </dg:DataGrid.Columns>

                <dg:DataGrid.RowsBackgroundColorPalette>
                    <dg:PaletteCollection>
                        <Color>#F2F2F2</Color>
                        <Color>#FFFFFF</Color>
                    </dg:PaletteCollection>
                </dg:DataGrid.RowsBackgroundColorPalette>
            </dg:DataGrid>

        </StackLayout>
    </ContentView>
</ContentPage>

恳请帮忙排查问题,感谢支持!

内容的提问来源于stack exchange,提问作者terry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 15:09:53