Xamarin Forms - Access a value of Multi-Dimentional Array from XAML Binding

Emixam23Emixam23 USMember ✭✭✭

Hi,

I don't know if it's possible but I'm trying to access an element of the double-dimensional array from my XAML file. First, Does it's possible?

I'm makin' a CustomCalendar by myself (Which works with WriteLine output). I'm trying now to make it graphics.

There is a part of the C# part:

public partial class Planning : ContentPage
{
    public class DayCase
    {
        public string Day { get; set; }

        public Color BackgroundColor { get; set; }
        public Color BorderColor { get; set; }
        public Color TextColor { get; set; }
    }
    public DayCase[][] Calendar; // It's the Grid in the Data way // Imagine it as a Grid

    DateTime today;
    private int _month;
    public int Month[...]
    public int Year;

    private void createCalendar()
    {
        Calendar = new DayCase[6][] 
        {
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()},
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()},
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()},
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()},
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()},
            new DayCase[7] { new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase(), new DayCase()}
        };
        today = DateTime.Now;
        _month = today.Month;
        Year = today.Year;
    }

    private void initCalendar()[...]

    // Here is an example to access data
    public Color GetDayBackgroundColor(string index)
    {
        return (Calendar[Convert.ToInt32(index[0])][Convert.ToInt32(index[1])].BackgroundColor);
    }
    public Color GetDayBorderColor(string index)[...]
    public Color GetDayTextColor(string index)[...]
    public string GetDayNumber(string index)[...]
}

Now from my XAML, I would like to access an Element of this GridData, Array etc. Example in the GetDayBackgroundColor() function.

XAML part:

<!-- Grid Planning part -->
<AbsoluteLayout BackgroundColor="White"
                AbsoluteLayout.LayoutBounds="0.5, 0.71, 1, 0.79"
                AbsoluteLayout.LayoutFlags="All">
  <Grid BackgroundColor="Transparent"
        AbsoluteLayout.LayoutBounds="0.5,0.95,0.9,0.7"
        AbsoluteLayout.LayoutFlags="All">
    <Grid.RowDefinitions>
      <RowDefinition Height="5*"/>
      <RowDefinition Height="15*"/>
      ...
      <RowDefinition Height="5*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="1*"/>
      <ColumnDefinition Width="14*"/>
      ...
      <ColumnDefinition Width="1*"/>          
    </Grid.ColumnDefinitions>

    <!-- Here is the interesting part -->
    <AbsoluteLayout Grid.Row="1" Grid.Column="1" BackgroundColor="Black">
      <Label Text="{Binding GetDayNumber {00}}" TextColor="White" BackgroundColor="Transparent" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
             AbsoluteLayout.LayoutBounds="0.5,0.5,1,1"
             AbsoluteLayout.LayoutFlags="All"/>
    </AbsoluteLayout>

  </Grid>
</AbsoluteLayout>

It doesn't work.. I also try Text="{Binding Path=Calendar[y][x].Day}" (where x&y are the position in the double dimensional array) but without success..

Does anyone know how can I handle it?

Thank in advance !

Answers

Sign In or Register to comment.