0
votes

Project is being made for an Android device.

I have searched for a while now and can't find an answer to why this happens.

<ListView x:Name="ScorebdList"
          Grid.Row="0"
          Grid.ColumnSpan="3"
          Grid.RowSpan="4"
          SelectionMode="None"
          Header="HIGHSCORES">
    <ListView.ItemTemplate>
        <DataTemplate >
            <ViewCell>
                <ViewCell.View>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                            <Label Text="{Binding Score}" HeightRequest="50" 
                                   MinimumHeightRequest="50" WidthRequest="50" 
                                   MinimumWidthRequest="50" HorizontalTextAlignment="Center" 
                                   BackgroundColor="Red"/>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Explanation:
Here I am trying to set the height and width of the Label and same for Grid. Red background on label to see where it goes.

This is not showing Label at all.

If I were to do this:

<ListView x:Name="ScorebdList"
          Grid.Row="0"
          Grid.ColumnSpan="3"
          Grid.RowSpan="4"
          SelectionMode="None"
          Header="HIGHSCORES"
          HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate >
            <ViewCell>
                <ViewCell.View>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                            <Label Text="{Binding Score}" HorizontalTextAlignment="Center" 
                                   BackgroundColor="Red"/>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I added HasUnevenRows attribute and set size and width in grid to auto. Also removed height/width requests of Label.

Now the Label will show, but not as I want it to.

Other things I tested:
I have also tried setting the Label inside a <StackLayout> instead of Grid with the same results.
(Inside the <ViewCell>)
Also tried using a <TextCell> instead of <ViewCell>, which I could not center but fix the height and width...

Question is:
Why? What am I missing?

Using the second code example is not how I want it to look. I want my <ListView> to show highscores in the center of the view with the height and width I set.

(Code below for the images have different colors than above code, Label is #D87040(orange ish) and grid background is #321F1F(Dark red)") Here are two images, first image is with width/height to '*'.

Width and Height is *

Here are second image where i set definition to 50 on w/h. Width and height are set to 50

4
try setting the height value of the Grid, not the Label. And/or set the RowHeight of the ListViewJason
Tried both. The thing is that when I do that the rows get the right height as wanted but the Label dissappears completely... and no score is shown.paperthins

4 Answers

0
votes

Your ListView has no ItemSource binding

0
votes

Use a Converter to change the Grid Row Height with the Listview RowHeight.

Converter:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }     

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

Xaml:

  <ContentPage.Resources>
    <ResourceDictionary>
        <local:MyConverter x:Key="MyConverter" />
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
    <ListView
        x:Name="ScorebdList"
        Grid.Row="0"
        Grid.RowSpan="4"
        Grid.ColumnSpan="3"
        Header="HIGHSCORES"
        RowHeight="100"
        SelectionMode="None"
        HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="{Binding Source={x:Reference ScorebdList}, Path=RowHeight, Converter={StaticResource MyConverter}}" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100" />
                            </Grid.ColumnDefinitions>
                            <Label
                                BackgroundColor="Red"
                                HorizontalTextAlignment="Center"
                                Text="{Binding score}" />
                        </Grid>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

Code Behind:

 public partial class Page17 : ContentPage
{
    public ObservableCollection<Score> scores { get; set; }
    public Page17()
    {
        InitializeComponent();
        scores = new ObservableCollection<Score>()
        {
            new Score{ score="Score1"},
            new Score{ score="Score2"},
            new Score{ score="Score3"},

        };
        ScorebdList.ItemsSource = scores;
        this.BindingContext = this;
    }
}
public class Score
{
    public string score { get; set; }
}
0
votes

From your code i see that you never set Grid.Column and Grid.Row to your label

<Label Text="{Binding Score}" HorizontalTextAlignment="Center" 
    BackgroundColor="Red" Grid.Column="0" Grid.Row="0"/>
0
votes

This problem were really strange, but thanks alot to you all who helped me!

In the end I used Margin="-50" to set the Grid. Still strange that i could not use height requests, row/column spacing... and even more.

But it is solved for now, thanks again!

With Margin="-50 0"

enter image description here