Share via

How to use "{Binding Source={x:Reference fontStepper}, Path=Value}" across pages

TazKing 85 Reputation points
2024-10-16T04:55:56.21+00:00

I'm binding to a Stepper control using the code below to set the font size on an Editor control on the same content view page. It works fine.

FontSize="{Binding Source={x:Reference fontStepper}, Path=Value}"

I want to use this to change font size on another content view page, but it doesn't compile. Compiler says it can't find the object referenced by fontStepper. I thought since all pages use the same namespace, it should be visible. What am I missing? Any help appreciated!

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

Answer accepted by question author

Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,166 Reputation points Microsoft External Staff
2024-10-16T07:25:45.7+00:00

Hello,

I want to use this to change font size on another content view page

This is an in-expectation error. Because your xaml code is actually equivalent to the following c# code.

test.BindingContext = this.fontStepper;
test.SetBinding(Label.FontSizeProperty, "Value");

As a result, when you try to bind to a control on another page in the same namespace, the compiler can't find an adapted binding property because they don't belong to the same class.

For cross-page binding, you can use the MVVM approach to bind a single instance of the same ViewModel across different pages. Please refer to the following steps and documentation.

Step 1. Create the Viewmodel for binding.

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;
 
    public void OnPropertyChanged([CallerMemberName] string name = "") =>
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    public int FontSizeValue
    {
        get
        {
            return fontSizeValue;
        }
        set
        {
            if (fontSizeValue != value)
            {
                fontSizeValue = value;
                OnPropertyChanged();
            }
        }
    }
    private int fontSizeValue;
    public MyViewModel()
    {
    }
}

Step 2. Set the BindingContext for the page to be bound.

Page for setting Fontsize.

<Slider x:Name="fontStepper" Value="{Binding FontSizeValue}" Minimum="15" Maximum="80"/>

public MainPage(MyViewModel myViewModel)
{
    InitializeComponent();
    this.BindingContext = myViewModel;
}

Page for using Fontsize.

<Label 
    Text="Welcome to .NET MAUI!"
    FontSize="{Binding FontSizeValue}"
    VerticalOptions="Center" 
    HorizontalOptions="Center" />

public NewPage1(MyViewModel myViewModel)
{
    InitializeComponent();
    this.BindingContext = myViewModel;
}

Step 3. Setting up a singleton for ViewModel through Dependency Injection technique in MauiProgram.cs file.

builder.Services.AddSingleton<MyViewModel>();
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<NewPage1>();

After that, you can verify that a change in value in the Slider changes the Fontsize of the other page.

You could refer to Dependency injection for more detailed information.

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.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.