passing data from view model not working

ahmed omar 181 Reputation points
2023-03-28T23:18:53.59+00:00

Hello ,

I have this text field

<Border BindingContext="{Binding Source={RelativeSource TemplatedParent}}"
                    StrokeThickness="0"
                    StrokeShape="RoundRectangle 10">
                    <Entry Text="{Binding FullName , Mode=TwoWay}" 
                       Margin="8,0,8,0"
                       BackgroundColor="{AppThemeBinding Light={StaticResource CardColorLight},
                            Dark={StaticResource CardColorDark}}"/>
                </Border>

and viewModel

 public SignUpViewModel()
        {
            Title = AppResources.SignUp;
            SignUpCommand = new Command(SignUp);  
            
        }
        private void SignUp(object obj)
        {
            var user = FulltName;
        }

and the SignupPage.xaml

 public SignUpPage()
        {
            InitializeComponent();
            BindingContext= new SignUpViewModel();
        }

when I type value in Text entry field, then click on Signup page, the FullName value will be null

Note : if I removed the border, it will work

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,920 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,301 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,231 Reputation points Microsoft Vendor
    2023-03-29T07:53:41.15+00:00

    Hello,

    I noticed that you used a Relative Binding method in Border, which caused the BindingContext to change to TemplatedParent inside the Border tag.

    Please refer to this official documentation to get more details: Relative bindings.

    Therefore, you need to delete BindingContext inside the Border to make the ViewModel work.

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful