Forum General
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

how can i bind a label in a third page to receive data from page one and two

raddradd Member ✭✭

i am new to Xamarin. in the app that a developing, the main-page contains three buttons that navigate to three popup pages. page one and two both contain a picker and the third page a label. i would like to know how can i bind the label in the third page to receive data from both page one and two depending on the page the user selected a value? so if the user navigate to page 1 and selects a value from the picker, the label should only display the value from page 1, same way if the user select a value from page 2 this same label should only display what was selected in page 2. it should always display one data value.
i hope its understandable.
thank you in advance

First page

<StackLayout>
        <Label Text="Laterality:" TextColor="Black"  FontSize="Default" ></Label>
        <Picker SelectedItem="{Binding PICKER1}" WidthRequest="120" FontSize="10" TextColor="Black" VerticalOptions="Center" BackgroundColor="WhiteSmoke">
            <Picker.Items>
                <x:String>15 cm</x:String>
                <x:String>30 cm</x:String>
                <x:String>45 cm</x:String>
                <x:String>60 cm</x:String>
                <x:String>75 cm</x:String>
                <x:String>90 cm</x:String>
            </Picker.Items>
        </Picker>

On the second page i have this

<StackLayout BackgroundColor="White">
        <Label Text="orientation:" TextColor="Black"  FontSize="Default" ></Label>
        <Picker SelectedItem="{Binding PICKER2}" WidthRequest="120" FontSize="10" VerticalOptions="Center" BackgroundColor="WhiteSmoke">
            <Picker.Items>
                <x:String>Vertical</x:String>
                <x:String>horizontal</x:String>
            </Picker.Items>
        </Picker> 

lastly on my last page i have a label which is suppose retrieve the values from the fist and second page depending on which page i did something on. so if the user selects something on page 1, the label should only display what is selected from page 1 and and viceversa.

        <Label Text="{Binding DisplayPICKER1}" Margin="5"  HorizontalOptions="StartAndExpand"  VerticalOptions="CenterAndExpand"></Label>

This is the MainViewModel. currently with this MainViewModel i need 2 labels, but i would like to use just one label

  string picker1 = string.Empty;
    public string PICKER1
    {
        get => picker1;
        set
        {
            if (picker1 == value)
                return;
            else
            {
                picker1 = value;
                OnPropertyChanged(nameof(PICKER1));
                OnPropertyChanged(nameof(DisplayPICKER1));
            }
        }
    }
    public string DisplayPICKER1 => $"laterality:{PICKER1}";


  string picker2 = string.Empty;
    public string PICKER2
    {
        get => picker2;
        set
        {
            if (picker2 == value)
                return;
            else
            {
                picker2 = value;
                OnPropertyChanged(nameof(PICKER2));
                OnPropertyChanged(nameof(DisplayPICKER2));
            }
        }
    }
    public string DisplayPICKER2 => $"Orientation:{PICKER2}";

baseview

public class baseview : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Best Answers

Answers

  • XamarinProblemsXamarinProblems Member ✭✭✭

    @radd I'm a bit confused as to what you specifically want, since the question asks for data between pages, but you also wonder how to merge two pickers. PICKER1 and PICKER2 and then i get worried i might be overcomplicating things. Could you add a screenshot or something drawn to give a more visual representation of what you what to have happen.

  • raddradd Member ✭✭

    thank you very much having for replying. the problem i have is that, i have one label and i would like to bind the 2 pickers to it but i would like the label to display only only one value. hence if picker 1 has a selected item then the label only display what was selected in picker 1 vice versa. the label should never display both values but always one. Picker 1 and 2 are all in different pages @XamarinProblems

  • XamarinProblemsXamarinProblems Member ✭✭✭

    @radd Then you should look into adding parameters to your page, such that you can update your Label to display your parameter value when you return to your main page from one of the picker pages. You cant simply bind the pickers cause there would be no way to know which picker value to display if both pickers had a value selected. The label should then be updated when your page either loads from new or reappears.

  • raddradd Member ✭✭

    This is very helpful thank you. could you please add some code to it for me to really understand if possible. @XamarinProblems.
    what if instead of one label i use two labels by binding picker1 to label1 and picker2 to label2 but then label1 hides if there is no selected item from picker 1 and label2 shows if there is a selected item from picker 2

  • raddradd Member ✭✭

    thank you very much. so i understood correctly the option of having two, label1 bound to picker1 and label2 buond to picker2 to show depending on which picker has a selected item is not possible?

  • raddradd Member ✭✭

    @XamarinProblems thank you very much really appreciate it. one last question it totally different from the previous one.
    so i have two pages, one contains a button and the other contains a label with with a text already. what i would like to dois when i click on the button in the first page i would like the text of the label in the second page to change.
    first page

    second page

  • XamarinProblemsXamarinProblems Member ✭✭✭

    @radd to do that you would need to either

    1. Page2 need a status in the constructor such that when you load your page you can change the label to either button pressed or not depending on the status you gave the constructor.

    2. Or save the state of the button (pressed/not pressed) in som sort of service that you can reach globally in your app such that when the page2 loads you can ask that service if the button was pressed or not and then alter the text depending on the answer from the service.

    hopefully that makes sence

  • raddradd Member ✭✭

    @XamarinProblems this makes sense. it will be very helpful if you can attach with some code. it will make me understand easier. i really appreciate your effort thou really

  • XamarinProblemsXamarinProblems Member ✭✭✭
    edited August 5

    @radd There is a lot of ways to do that, in your case i think option 1 is the better so take a look at:

    Demo project: https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/PassingData
    Xamarin explaining: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical

Sign In or Register to comment.