0

I cant work out how best to solve this and bare with me, I am a beginner.

I have a list of "questions" built within a dynamic grid in code. Each question requires a string answer to be stored in "PassFixFail". The options/radio buttons are: Pass, Fix and Fail.

How do I store each answer as the string selected? I am currently binding the result but its coming back as a boolean. I know why it is but I am unsure how to achieve my intention.

Thank you

Here is by c#

var stack = new StackLayout();

var myFrame = new Frame();
myFrame.BorderColor = (Color)Xamarin.Forms.Application.Current.Resources["BodyBackground"];
myFrame.CornerRadius = 2;
myFrame.Margin = new Thickness(5, 5, 5, 5);
myFrame.Padding = 2;

//Set up
var grid = new Xamarin.Forms.Grid();
grid.Padding = 10;
grid.HorizontalOptions = LayoutOptions.FillAndExpand;
grid.RowDefinitions = new RowDefinitionCollection
        {
            new RowDefinition { Height = GridLength.Star },
        };
grid.ColumnDefinitions = new ColumnDefinitionCollection
        {
            new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) },
            new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
            new ColumnDefinition { Width = new GridLength(40, GridUnitType.Absolute) },
            new ColumnDefinition { Width = new GridLength(40, GridUnitType.Absolute) },
            new ColumnDefinition { Width = new GridLength(40, GridUnitType.Absolute) },
        };

//Header Row                
int rowCount = 0;

grid.Children.Add(GridLabel("col1", true), 0, rowCount);
grid.Children.Add(GridLabel("col2", true), 1, rowCount);
grid.Children.Add(GridLabel("Pass", true), 2, rowCount);
grid.Children.Add(GridLabel("Fix", true), 3, rowCount);
grid.Children.Add(GridLabel("Fail", true), 4, rowCount);

rowCount++;

foreach(var van in myModelList)
{ 
    grid.Children.Add(GridDataLabel("col1", inputModel), 0, rowCount);
    grid.Children.Add(GridDataLabel("col2", inputModel), 1, rowCount);

    grid.Children.Add(GridRadioButton(rowCount, inputModel, "Pass", "PassFixFail"), 2, rowCount);
    grid.Children.Add(GridRadioButton(rowCount, inputModel, "Fix", "PassFixFail"), 3, rowCount);
    grid.Children.Add(GridRadioButton(rowCount, inputModel, "Fail", "PassFixFail"), 4, rowCount);

    rowCount++;
}                

stack.Children.Add(grid);
myFrame.Content = stack;

myGridContainer.Children.Add(myFrame);

Then the method Im using is

private RadioButton GridRadioButton(int rowIndex, MyModel input, string content, string prop)
{
    RadioButton radio = new RadioButton();
    radio.GroupName = "GroupName_" + rowIndex;
    radio.Value = content;         
    radio.SetBinding(RadioButton.IsCheckedProperty, new Binding(prop, BindingMode.TwoWay, source: input));
    radio.BindingContext = input;
    return radio;
}

For "prop" as my param, I have also tried changing that to corresponding boolean properties within the model and then doing the following:

public bool PassChecked
{
    get
    {
        return (this.PassFixFail == "Pass");
    }
    set
    {
        if (value == true)
        {
            PassFixFail = "Pass";
        }
    }
}
public bool FixChecked
{
    get
    {
        return (this.PassFixFail == "Fix");
    }
    set
    {
        if (value == true)
        {
            PassFixFail = "Fix";
        }
    }
}
public bool FailChecked
{
    get
    {
        return (this.PassFixFail == "Fail");
        }
        set
        {
            if (value == true)
            {
                PassFixFail = "Fail";
            }
        }
    }

I have also tried adding the attribute RadioButtonGroup.SelectedValue to a stack and adding the radio buttons to there but nothing either:

st.SetBinding(RadioButtonGroup.SelectedValueProperty, new Binding("PassFixFail", BindingMode.TwoWay, source: inputModel));

This however didn't work. I feel like somewhere I am doing something incorrectly or missing something obvious.

Thank you in advance

PS. Its likely someone may mention that Xamarin needs updating to .NET MAUI. This will be done for now we need to stick to this for timescale purposes

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.