Hello,
For the problem that some data of one page needs to respond to data changes of another page, it is recommended that you use the singleton ViewModel to bind the data of the two pages.
You can refer to the steps for the solution in Collection view mirror to list view and the following sample code.
For the ViewModel:
public class CommonViewModel : INotifyPropertyChanged
{
private static CommonViewModel instance = null;
// This is a simple singleton pattern
public static CommonViewModel Instance
{
get
{
if (instance == null)
{
instance = new CommonViewModel();
}
return instance;
}
}
private CommonViewModel()
{
Names = new ObservableCollection<string>();
Label_Text = "test";
Names.Add("a");
Names.Add("b");
Names.Add("c");
Names.Add("d");
Names.Add("e");
}
private string selectedItem;
public string SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem != value)
{
selectedItem = value;
//After selecting the string in Picker, update it to Label.
Label_Text = selectedItem;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private string label_text;
public string Label_Text
{
get { return label_text; }
set
{
if (label_text != value)
{
label_text = value;
OnPropertyChanged();
}
}
}
private ObservableCollection<string> names;
public ObservableCollection<string> Names
{
get { return names; }
set
{
if (names != value)
{
names = value;
OnPropertyChanged();
}
}
}
}
For Page2, I used a Picker to complete the selection of data.
<Picker x:Name="picker" ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedItem}"
Title="Select a monkey"
TitleColor="Red"></Picker>
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.