网格自动隐藏在 xaml Xamarin Forms 中

Grid auto hiding in xaml Xamarin Forms

我遇到了一个奇怪的错误/错误。我在网格内有 2 个按钮(保存和清除),当用户不向字段输入任何数据时,它会显示打印在每个控件底部的标签中的错误。 我使用了滚动视图,如果设计转到滚动,那么我在网格内的按钮会自动隐藏。但是当我 select 任何条目中的任何选择器或焦点时,页面中可见的网格(保存并清除)。 我没有在选择器 selected 索引更改中编写任何代码。 我正在分享我的代码。并附上页面的屏幕截图提前致谢。

<ContentPage.Content>

    <ScrollView x:Name="MyScroll">

        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="10">

        <StackLayout>

            <Entry Text="{Binding BindProductInvModel.ProductInvID}" Margin="0" FontSize="1" IsVisible="false" HorizontalOptions="FillAndExpand" />

            <Label Text="Product Name" Margin="2,-2" FontAttributes="Bold" />
            <Picker x:Name="ProductName" Margin="2,-10,2,5" HorizontalOptions="FillAndExpand" 
                    ItemsSource="{Binding BindProductList}" 
                    ItemDisplayBinding="{Binding ProductName}" 
                    SelectedItem="{Binding BindSelectedProduct,Mode=TwoWay}" 
                    Title="{Binding BindCommonDisplayName.PickerTitleDisplayName}"/>
            <Label x:Name="ProductIDError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />

            <Label Text="Quanity" Margin="2,-10" FontAttributes="Bold" />

            <Entry x:Name="Quantity"  Text="{Binding BindProductInvModel.Quantity}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="QuantityError" Margin="2,-10,2,5" 
                   TextColor="Red" IsVisible="false" FontAttributes="Italic" />


            <Label Text="Unit" Margin="2,-10" FontAttributes="Bold" />
            <Entry x:Name="Unit"  Text="{Binding BindProductInvModel.Unit}" IsReadOnly="True"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="UnitError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />

            <Label Text="Vendor Name" Margin="2,-10" FontAttributes="Bold" />
            <Picker x:Name="VendorName" Margin="2,-5,2,5" ItemsSource="{Binding BindVendorList}" 
                    ItemDisplayBinding="{Binding VendorName}" 
                    SelectedItem="{Binding BindSelectedVendor,Mode=TwoWay}" 
                    Title="{Binding BindCommonDisplayName.PickerTitleDisplayName}" HorizontalOptions="FillAndExpand" />
            <Label x:Name="VendorIDError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />


            <Label Text="Receiving Date" Margin="2,-12" FontAttributes="Bold" />

            <DatePicker x:Name="PickerRecvDate" Margin="2,-5,2,5" Date="{Binding BindProductInvModel.ReceivingDate,Mode=TwoWay}" 
                        HorizontalOptions="FillAndExpand" Format="dd-MM-yyyy"  />
         
            <Label Text="Price" Margin="2,-12" FontAttributes="Bold" />
            <Entry x:Name="Price"  Text="{Binding BindProductInvModel.Price}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="PriceError" Margin="2,-10,2,5" 
                   TextColor="Red" IsVisible="false" FontAttributes="Italic" />


            <Label Text="Fair" Margin="2,-12" FontAttributes="Bold" />
            <Entry x:Name="Fair"  Text="{Binding BindProductInvModel.Fair}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="FairError" Margin="2,-10,2,5" 
                   TextColor="Red" IsVisible="false" FontAttributes="Italic" />


            <Label Text="OtherCharges" Margin="2,-12" FontAttributes="Bold" />
            <Entry x:Name="OtherCharges" Text="{Binding BindProductInvModel.OtherCharges}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="OtherChargesError" Margin="2,-10,2,5" 
                   TextColor="Red" IsVisible="false" FontAttributes="Italic" />


            <Label Text="Total" 
                   Margin="2,-12" FontAttributes="Bold" />
            <Entry Text="{Binding BindProductInvModel.Total}"
                       Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next" IsEnabled="False"
                       HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
            <Label x:Name="TotalError" Margin="2,-10,2,5" 
                   TextColor="Red" IsVisible="false" FontAttributes="Italic" />

      </StackLayout>


            <StackLayout>

                <Grid HorizontalOptions="FillAndExpand" Margin="2,-12">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
             </Grid.ColumnDefinitions>

            <Button Text="Save" x:Name="BtnSave" 
                        HorizontalOptions="FillAndExpand" CornerRadius="10" 
                        BorderWidth="2" BackgroundColor="#ff6633" TextColor="#fff" 
                        Margin="2" Grid.Column="0" Grid.Row="0" Command="{Binding SaveCommand}" />

            <Button Text="Clear" x:Name="BtnClear" HorizontalOptions="FillAndExpand" 
                        CornerRadius="10" BorderWidth="2" BackgroundColor="#bfbfbf" 
                        TextColor="#fff" Margin="2" Grid.Column="1" Grid.Row="0" Command="{Binding ClearCommand}" />
        </Grid>

            </StackLayout>


        </StackLayout>
        
        
    </ScrollView>

</ContentPage.Content>

如果您想让带有按钮的网格在滚动或select任何选择器时始终可见,您可以将按钮置于滚动视图之外。

 <StackLayout> 
 <ScrollView x:Name="MyScroll">

    <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="10">

    <StackLayout>

        <Entry Text="{Binding BindProductInvModel.ProductInvID}" Margin="0" FontSize="1" IsVisible="false" HorizontalOptions="FillAndExpand" />

        <Label Text="Product Name" Margin="2,-2" FontAttributes="Bold" />
        <Picker x:Name="ProductName" Margin="2,-10,2,5" HorizontalOptions="FillAndExpand" 
                ItemsSource="{Binding BindProductList}" 
                ItemDisplayBinding="{Binding ProductName}" 
                SelectedItem="{Binding BindSelectedProduct,Mode=TwoWay}" 
                Title="{Binding BindCommonDisplayName.PickerTitleDisplayName}"/>
        <Label x:Name="ProductIDError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />

        <Label Text="Quanity" Margin="2,-10" FontAttributes="Bold" />

        <Entry x:Name="Quantity"  Text="{Binding BindProductInvModel.Quantity}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="QuantityError" Margin="2,-10,2,5" 
               TextColor="Red" IsVisible="false" FontAttributes="Italic" />


        <Label Text="Unit" Margin="2,-10" FontAttributes="Bold" />
        <Entry x:Name="Unit"  Text="{Binding BindProductInvModel.Unit}" IsReadOnly="True"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="UnitError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />

        <Label Text="Vendor Name" Margin="2,-10" FontAttributes="Bold" />
        <Picker x:Name="VendorName" Margin="2,-5,2,5" ItemsSource="{Binding BindVendorList}" 
                ItemDisplayBinding="{Binding VendorName}" 
                SelectedItem="{Binding BindSelectedVendor,Mode=TwoWay}" 
                Title="{Binding BindCommonDisplayName.PickerTitleDisplayName}" HorizontalOptions="FillAndExpand" />
        <Label x:Name="VendorIDError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />


        <Label Text="Receiving Date" Margin="2,-12" FontAttributes="Bold" />

        <DatePicker x:Name="PickerRecvDate" Margin="2,-5,2,5" Date="{Binding BindProductInvModel.ReceivingDate,Mode=TwoWay}" 
                    HorizontalOptions="FillAndExpand" Format="dd-MM-yyyy"  />
     
        <Label Text="Price" Margin="2,-12" FontAttributes="Bold" />
        <Entry x:Name="Price"  Text="{Binding BindProductInvModel.Price}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="PriceError" Margin="2,-10,2,5" 
               TextColor="Red" IsVisible="false" FontAttributes="Italic" />


        <Label Text="Fair" Margin="2,-12" FontAttributes="Bold" />
        <Entry x:Name="Fair"  Text="{Binding BindProductInvModel.Fair}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="FairError" Margin="2,-10,2,5" 
               TextColor="Red" IsVisible="false" FontAttributes="Italic" />


        <Label Text="OtherCharges" Margin="2,-12" FontAttributes="Bold" />
        <Entry x:Name="OtherCharges" Text="{Binding BindProductInvModel.OtherCharges}" Unfocused="Entry_Unfocused" Focused="Entry_Focused"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="OtherChargesError" Margin="2,-10,2,5" 
               TextColor="Red" IsVisible="false" FontAttributes="Italic" />


        <Label Text="Total" 
               Margin="2,-12" FontAttributes="Bold" />
        <Entry Text="{Binding BindProductInvModel.Total}"
                   Margin="2,-5,2,5" WidthRequest="150" ReturnType="Next" IsEnabled="False"
                   HorizontalOptions="FillAndExpand" Keyboard="Numeric" />
        <Label x:Name="TotalError" Margin="2,-10,2,5" 
               TextColor="Red" IsVisible="false" FontAttributes="Italic" />

  </StackLayout>
   
    </StackLayout>
    
    
</ScrollView>
  <StackLayout>

            <Grid HorizontalOptions="FillAndExpand" Margin="2,-12">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
         </Grid.ColumnDefinitions>

        <Button Text="Save" x:Name="BtnSave" 
                    HorizontalOptions="FillAndExpand" CornerRadius="10" 
                    BorderWidth="2" BackgroundColor="#ff6633" TextColor="#fff" 
                    Margin="2" Grid.Column="0" Grid.Row="0" Command="{Binding SaveCommand}" />

        <Button Text="Clear" x:Name="BtnClear" HorizontalOptions="FillAndExpand" 
                    CornerRadius="10" BorderWidth="2" BackgroundColor="#bfbfbf" 
                    TextColor="#fff" Margin="2" Grid.Column="1" Grid.Row="0" Command="{Binding ClearCommand}" />
    </Grid>

        </StackLayout>

</StackLayout>