Ezekiel
Ezekiel
CC#
Created by Ezekiel on 7/13/2023 in #help
❔ Populate a grid .NET MAUI
Hello guys, I want to create a grid following some logic but I can't find a solution. I use 2 bindableProperties to know how many rows and columns I should create, like so
public static readonly BindableProperty OptionsProperty = BindableProperty.Create(
nameof(Options), typeof(List<Option>), typeof(LikertMultiple), propertyChanged: (bindable, oldValue, newValue) =>
{
var control = (LikertMultiple)bindable;
control.OptionsLabels.ItemsSource = (List<Option>)newValue;

control.columns = new List<ColumnDefinition>();
for (int i = 0; i < control.NumberOfColumns; i++)
{
ColumnDefinition col = new ColumnDefinition();
control.columns.Add(col);
control.myGrid.ColumnDefinitions.Add(col);
}
});

public static readonly BindableProperty RowsProperty = BindableProperty.Create(
nameof(Rows), typeof(List<LikertQuestion>), typeof(LikertMultiple), propertyChanged: (bindable, oldValue, newValue) =>
{
//Same for rows
}
});

public List<Option> Options
{
get => (List<Option>)GetValue(OptionsProperty);
set => SetValue(OptionsProperty, value);
}

public List<LikertQuestion> Rows
{
//same
}
public static readonly BindableProperty OptionsProperty = BindableProperty.Create(
nameof(Options), typeof(List<Option>), typeof(LikertMultiple), propertyChanged: (bindable, oldValue, newValue) =>
{
var control = (LikertMultiple)bindable;
control.OptionsLabels.ItemsSource = (List<Option>)newValue;

control.columns = new List<ColumnDefinition>();
for (int i = 0; i < control.NumberOfColumns; i++)
{
ColumnDefinition col = new ColumnDefinition();
control.columns.Add(col);
control.myGrid.ColumnDefinitions.Add(col);
}
});

public static readonly BindableProperty RowsProperty = BindableProperty.Create(
nameof(Rows), typeof(List<LikertQuestion>), typeof(LikertMultiple), propertyChanged: (bindable, oldValue, newValue) =>
{
//Same for rows
}
});

public List<Option> Options
{
get => (List<Option>)GetValue(OptionsProperty);
set => SetValue(OptionsProperty, value);
}

public List<LikertQuestion> Rows
{
//same
}
So this works, like it creates a grid and if i put some new elements in the grid such as
myGrid.setColumn(text, 1);
myGrid.setRow(text, 1);
myGrid.setColumn(text, 1);
myGrid.setRow(text, 1);
it works But what I can't understand is that the columns and rows lists are null and if i do something like
myGrid.setColumn(columns.Count -1, 1);
myGrid.setRow(rows.Count -1, 1);
myGrid.setColumn(columns.Count -1, 1);
myGrid.setRow(rows.Count -1, 1);
it won't work. Thank you
2 replies
CC#
Created by Ezekiel on 7/12/2023 in #help
❔ Question for when using XAML vs C#
Hello ! So I'm programming in video Games and recently I learned .NET MAUI which is really different with XAML. And to me it is confusing when to use. As I understand the best thing to do is to use it as often as possible but in some cases I don't know how to do so. Here is my example : I am doing some questionnaire creator and I want my user to chose as many options he wants for its question (like 5 radio buttons or 4). Then I want to instantiate all those buttons but I don't know what is the good practice. Like should I pass some parameter of number and then in my component constructor instantiate the good amount ? Or is there some XAML possibility to know how much of the same item it has to display ? Or should I just create a stackPannel and add as many component as needed in my RelayCommand ? Thank you for your help, it is highly appreciated.
6 replies