加载Xamarin.Forms.DataGrid时的堆栈布局

btxsgosb  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(169)

最近我在自己的Xamarin项目中使用了Xamarin.Forms.DataGrid。
当我在StackLayout中插入多个组件时,我发现datagrid只显示标题之外的数据。
有人知道我做错了什么吗?
XAML代码发布在以下位置:

<?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>

谢谢你的帮助!
特里·比尔
此外,我已经尝试了Jason的建议,添加高度值和VerticalOption值如下,但结果仍然是一样的。

<?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>
piah890a

piah890a1#

您可以尝试在ContentPage中添加一个StackLayout,并将组件和DataGrid作为它的子组件。
您还需要为您的ContentView设置HeightRequest
请参考以下代码:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
             x:Class="XFDataGrid.MainPage">

    <StackLayout VerticalOptions="StartAndExpand"  Orientation="Vertical" >

       <Button  Text="test" />

        <ContentView BackgroundColor="Yellow" HeightRequest="1000">

            <!-- Place new controls here -->
            <dg:DataGrid ItemsSource="{Binding Professionals}" SelectionEnabled="True" SelectedItem="{Binding SelectedProfesstional}" RowHeight="70" HeaderHeight="50"
                    BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" ActiveRowColor="#8899AA">
                <!--<x:Arguments>
                <ListViewCachingStrategy>RetainElement</ListViewCachingStrategy>
            </x:Arguments>-->
                <!--<dg:DataGrid.HeaderFontSize>
                <OnIdiom x:TypeArguments="x:Double">
                    <OnIdiom.Tablet>15</OnIdiom.Tablet>
                    <OnIdiom.Phone>12</OnIdiom.Phone>
                </OnIdiom>
            </dg:DataGrid.HeaderFontSize>-->
                <dg:DataGrid.Columns>
                    <dg:DataGridColumn Title="ID" PropertyName="Id" Width="1*"></dg:DataGridColumn>
                    <dg:DataGridColumn PropertyName="Name" Width="2*">
                        <dg:DataGridColumn.FormattedTitle>
                            <FormattedString>
                                <Span Text="Name" FontSize="13" TextColor="Black" FontAttributes="Bold"/>
                            </FormattedString>
                        </dg:DataGridColumn.FormattedTitle>
                    </dg:DataGridColumn>
                    <dg:DataGridColumn Title="Desigination" PropertyName="Desigination" Width="2*"/>
                    <dg:DataGridColumn Title="Domain" PropertyName="Domain" Width="2*"/>
                    <dg:DataGridColumn Title="Exp" PropertyName="Experience" Width="1*"/>
                </dg:DataGrid.Columns>
                <dg:DataGrid.RowsBackgroundColorPalette>
                    <dg:PaletteCollection>
                        <Color>#F2F2F2</Color>
                        <Color>#FFFFFF</Color>
                    </dg:PaletteCollection>
                </dg:DataGrid.RowsBackgroundColorPalette>
            </dg:DataGrid>

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

相关问题