Hello,
For a custom popup using MVVM with data binding, please refer to the following official documentation and examples.
In this example, it demonstrates how to bind the text in the Label and the button click event to the ViewModel, and pass the Text property in the Label as a parameter to the ViewModel.
In xaml:
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MauiApp1"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MauiApp1.NewPage1">
<toolkit:Popup.BindingContext>
<vm:TestViewModel/>
</toolkit:Popup.BindingContext>
<StackLayout WidthRequest="1000" HeightRequest="500">
<Label x:Name="test_label" Text="{Binding Test}"/>
<Button Text="test" Command="{Binding TestCommand}" CommandParameter="{Binding Source={x:Reference test_label}, Path=Text}"/>
</StackLayout>
</toolkit:Popup>
In ViewModel:
class TestViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string test;
public string Test
{
get { return test; }
set
{
if (test != value)
{
test = value;
OnPropertyChanged();
}
}
}
public ICommand TestCommand { get; private set; }
public TestViewModel()
{
Test = "Label Text";
TestCommand = new Command<string>(x => WriteLabel(x));
}
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private void WriteLabel(object str)
{
if (str != null)
{
var s = str.ToString();
Console.WriteLine(s);
}
}
}
You could get a comprehensive understanding of how MVVM works in MAUI by referring to the above documents and examples. Then, you could create your own custom MVVM pop-ups.
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.