Search code examples
xamarin.formsnavigationradio-buttonorientation

xamarin radio buttons not displaying


I have a content page with the following data template for RadioButtons:

            <DataTemplate x:Key="RadioTemplate">
                <StackLayout Padding="0,2,0,0">
                    <StackLayout 
                            Orientation="Horizontal" >
                        <Label
                                Text="{Binding Title}"
                                Style="{StaticResource LabelStyle}">
                            </Label>
                            <ImageButton HorizontalOptions="Center" 
                                CornerRadius="6"
                                VerticalOptions="Center"
                                Clicked="HelpButton_Clicked"
                                HeightRequest="12"
                                WidthRequest="12"
                                BorderColor="Black"
                                BackgroundColor="Black"
                                BorderWidth="1"
                                IsVisible="{Binding ShowHelpButton}">
                                <ImageButton.Source>
                                    <FontImageSource FontFamily="FAFreeSolid"
                                    Color="White"                                         
                                    Glyph="{x:Static fa:FontAwesomeIcons.Question}"/>
                                </ImageButton.Source>
                            </ImageButton>
                        </StackLayout>
                        <Label
                            Style="{StaticResource HelpStyle}"
                            Text="{Binding HelpTopic.Text}"
                            IsVisible="{Binding HelpTopic.IsVisible, Mode=TwoWay}">
                        </Label>
                        <StackLayout 
                            RadioButtonGroup.GroupName="{Binding ChoiceList.Code}"
                            RadioButtonGroup.SelectedValue="{Binding Value}"
                            BindableLayout.ItemsSource="{Binding ChoiceList.Choices}"
                            BindableLayout.EmptyView="No choices available"
                            Orientation="Horizontal"
                            Margin="10,0,0,0">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>
                                    <RadioButton Value="{Binding Value}"
                                        Content="{Binding Label}"
                                        IsChecked="{Binding IsSelected}"
                                        CheckedChanged="OnRadioChanged"
                                        FlowDirection="LeftToRight">
                                    </RadioButton>
                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                    </StackLayout>
                </StackLayout>
            </DataTemplate>

It is called from a collection view on the content page:

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <StackLayout>
                <StackLayout Orientation="Horizontal">
                    <Label 
                        Text="Work Order Request" 
                        HorizontalTextAlignment="Start"
                        HorizontalOptions="StartAndExpand"
                        VerticalTextAlignment="Center"
                        TextColor="White" 
                        FontSize="Large"/>
                    <Button Text="Back" Clicked="BackButton_Clicked" HorizontalOptions="End" />
                </StackLayout>

                <Label 
                    Padding="0,2,0,0"
                    Text="Verify your data before submitting" 
                    HorizontalTextAlignment="Start" 
                    TextColor="White" 
                    FontSize="Micro" />
            </StackLayout>
        </Frame>
        <Label
            Margin="10,0,0,0"
                Grid.Column="0"
                Text="SOME TEXT HERE"
                HorizontalOptions="Start"
                FontSize="Micro"/>
        <CollectionView 
            ItemsSource="{Binding MainPageView.Fields}"                        
            EmptyView="No fields for this screen."
            ItemTemplate="{StaticResource templateSelector}"
            SelectionChanged="CollectionView_SelectionChanged">
        </CollectionView>
        <StackLayout Margin="10,0,10,5" Orientation="Vertical">
            <Button Command="{Binding MainPageView.SubmitCommand}" Text="Submit" />
            <validator:ErrorSummaryView 
                IsVisible="{Binding MainPageView.ShowValidationSummary, Mode=OneWay}"
                ErrorStateManager="{Binding MainPageView.ErrorStateManager, Mode=OneWay}"
                Margin="0,0,0,5"/>
        </StackLayout>
        
    </StackLayout>

If I have the App.xaml.cs instantiate the view directly, it displays fine:

        public App()
        {
            InitializeComponent();

            MainPage = new MainPage(false);
        }

However, if I change and add a landing page that has a button to redirect to that page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Test.Ui.Dashboard">
    <ContentPage.Content>
        <StackLayout>
            <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
                <StackLayout>
                    <Label 
                        Text="TEST DASHBOARD" 
                        HorizontalTextAlignment="Start" 
                        VerticalTextAlignment="Center"
                        TextColor="White" 
                        FontSize="Large"/>
                    <Label 
                    Padding="0,2,0,0"
                    Text="Sample UI/UX Pages" 
                    HorizontalTextAlignment="Start" 
                    TextColor="White" 
                    FontSize="Micro" />
                </StackLayout>
            </Frame>
            <StackLayout Orientation="Horizontal">
                <Button Text="Work Order" Clicked="WorkOrder_Clicked" HorizontalOptions="FillAndExpand" BackgroundColor="#2196F3" TextColor="White" FontAttributes="Bold" FontSize="Large" CornerRadius="10"/>
                <Button Text="Randomized Field Order" Clicked="Randomized_Clicked" HorizontalOptions="FillAndExpand" BackgroundColor="#2196F3" TextColor="White" FontAttributes="Bold" FontSize="Large" CornerRadius="10" />
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Test.Ui
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Dashboard : ContentPage
    {
        public Dashboard()
        {
            InitializeComponent();
        }

        private void WorkOrder_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new MainPage(false));
        }

        private void Randomized_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new MainPage(true));
        }
    }
}

and change App.xaml.cs to 

using Xamarin.Forms;

namespace Test.Ui
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage( new Dashboard());
        }

        protected override void OnStart()
        {
            // load up the style xaml files here
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}

Now, when I click the Work Order button, the radio options aren't displayed. If I change the stacklayout for the radio options to veritical orientation, they do. So the data is there but the view isn't able to render when I'm in a NavigationPage. Thoughts? I'd really like to have my radio buttons be horizontal when there aren't many choices.

Thanks


Solution

  • In my data template, I had to add an explicit WidthRequest = 150

                            <StackLayout 
                                RadioButtonGroup.GroupName="{Binding ChoiceList.Code}"
                                RadioButtonGroup.SelectedValue="{Binding Value}"
                                BindableLayout.ItemsSource="{Binding ChoiceList.Choices}"
                                BindableLayout.EmptyView="No choices available"                            
                                Orientation="Horizontal"
                                Margin="10,0,0,0">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <RadioButton Value="{Binding Value}"
                                            Content="{Binding Label}"                                        
                                            IsChecked="{Binding IsSelected}"
                                            VerticalOptions="Center"
                                            IsClippedToBounds="False"
                                            HeightRequest="27"
                                            WidthRequest="150"
                                            CheckedChanged="OnRadioChanged">
                                        </RadioButton>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>
    

    I guess the StackLayout wasn't able to calculate how wide to make each one. Will see if I can figure out a different way to do it so it adjusts for the width of the content.