Xamarin.forms 表单:覆盖代码中第6行网格的高度

Xamarin.forms 表单:覆盖代码中第6行网格的高度,xamarin.forms,Xamarin.forms,我有一个网格: <!--MainGrid--> <Grid Grid.Row="2" x:Name="maingrid_piccat"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*

我有一个网格:

            <!--MainGrid-->
            <Grid Grid.Row="2" x:Name="maingrid_piccat">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                         Aspect="AspectFit"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Picker x:Name="picker_maincat_pickcate" 
                            Grid.Column="1"
                            Title="Hauptkategorie"
                            FontFamily="arial"
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                        <Picker.Items>
                            <x:String>Cat 1</x:String>
                            <x:String>Cat 2</x:String>
                            <x:String>Cat 3</x:String>
                            <x:String>Cat 4</x:String>
                        </Picker.Items>
                        </Picker>

                </Grid>

                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                         Aspect="AspectFit"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Picker x:Name="picker_subcat1_pickcate" 
                            Grid.Column="1"
                            Title="Unterkategorie 01"
                            FontFamily="arial"
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                        <Picker.Items>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                        </Picker.Items>
                    </Picker>

                </Grid>

                <Grid Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                         Aspect="AspectFit"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Picker x:Name="picker_subcat2_pickcate" 
                            Grid.Column="1"
                            FontFamily="arial"
                            Title="Unterkategorie 02"
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                        <Picker.Items>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                        </Picker.Items>
                    </Picker>

                </Grid>

                <Grid Grid.Row="3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                         Aspect="AspectFit"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Picker x:Name="picker_subcat3_pickcate" 
                            Grid.Column="1"
                            FontFamily="arial"
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            Title="Unterkategorie 03"                                
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                        <Picker.Items>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                            <x:String>Subcat yx</x:String>
                        </Picker.Items>
                    </Picker>

                </Grid>

                <Grid Grid.Row="4" x:Name="row4_piccat">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                        x:Name="btn_dummy_piccat"
                         Aspect="AspectFit"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Entry x:Name="entry_name_pickcat" 
                            Grid.Column="1"
                            FontFamily="arial"
                           MaxLength="15"
                           Placeholder="Titel der Anzeige" 
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                    </Entry>

                </Grid>

                <Grid Grid.Row="5" x:Name="row5_piccat">
                    <Grid.ColumnDefinitions >
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*" />
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                         Aspect="AspectFit"
                         x:Name="btn_dummy_piccat2"
                         Grid.Column="1"
                         Source="btn_emptydummy.png" BackgroundColor="#00000000"/>

                    <Entry x:Name="entry_descri_pickcate" 
                            Grid.Column="1"
                            FontFamily="arial"
                           MaxLength="50"
                           Placeholder="Kurzbeschreibung" 
                            BackgroundColor="#00000000"
                            TextColor="#272727"
                            HorizontalOptions="Center" 
                            VerticalOptions="Center">
                    </Entry>

                </Grid>

                <Grid Grid.Row="6">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageButton
                        Margin="10,10,10,10"
                        Grid.Column="1"
                        Aspect="AspectFit"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                x:Name="name_btnfoto_pickcat"
                BackgroundColor="#00000000"
                Source="fotobutton.png" />



                </Grid>

            </Grid>

        </Grid>
正如yopu所看到的,我试图改变行的定义,但它似乎没有影响网格。 结果如下:

如您所见,文本被截断,因为网格行太小。 我如何在代码中改变它显示整个字符串的高度


谢谢

您可以尝试使用
Grid.RowSpan
并将文本所在的位置设置为2。它的作用是将行span设置为提供的数字,并将VisualElement从
网格.Row
中提供的起始行span设置为
网格.RowSpan
中提供的行数

如果您有进一步的疑问,请告诉我

更新:- Xaml

转换器:-

public class BoolToIntRowSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            else if ((bool)value)
                return 2;
            else return 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

因此,每当bool设置为true或false时,Row.Span将相应地更改。

您可以在RowDefinition.Height上创建绑定,通过viewmodel更改高度

Xaml

您好,这可以在xaml中使用,但不能在代码中使用。我需要它在运行时运行。我之前遇到过类似的问题,我在ViewModel中使用Bool和转换器解决了它。请查看更新的答案。
Grid.RowSpan="{Binding IsTwoRowVisible, Converter={StaticResource BoolToIntRowSpanConverter}}"
public class BoolToIntRowSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            else if ((bool)value)
                return 2;
            else return 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="{Binding Height}"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label Grid.Row="0" BackgroundColor="Red"></Label>
    <Label Grid.Row="1" BackgroundColor="Green"></Label>
    <Label Grid.Row="2" BackgroundColor="Blue"></Label>
</Grid>
public class MyModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private GridLength height;
    public GridLength Height { 
        get {
            return height;
        } 
        set {

            height = value;
            NotifyPropertyChanged();
        } }

    public MyModel()
    {
        Height = new GridLength(1, GridUnitType.Star);
    }
}

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page8 : ContentPage
{

    public MyModel model; 

    public Page8()
    {
        InitializeComponent();

        model = new MyModel();


        this.BindingContext = model;

    }




    private void ToolbarItem_Clicked(object sender, EventArgs e)
    {
        model.Height = new GridLength(1, GridUnitType.Auto);
    }
}