user2474503
user2474503

Reputation: 5

.Net Maui custom entry field not passing value from view model

I have a custom control Entry field , when I use this control in signup view and pass a value , this value will be null even I have enter a value

custom control Entry field is

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BlogTemplate.ControlTemplates.ChicEntry">

    <ContentView.Resources>

        <ControlTemplate x:Key="ChicEntryControlTemplate">

            <Border 
                    StrokeThickness="0"
                    StrokeShape="RoundRectangle 10">

                <Entry Text="{Binding Text, Mode=TwoWay}" 
                       Placeholder="{Binding  Placeholder, Mode=TwoWay}" 
                       Margin="8,0,8,0"
                       BackgroundColor="{AppThemeBinding Light={StaticResource CardColorLight},
                            Dark={StaticResource CardColorDark}}"/>
                
            </Border>
    
        </ControlTemplate>
        
    </ContentView.Resources>
    
</ContentView>

with c# backend

using static System.Net.Mime.MediaTypeNames;

namespace BlogTemplate.ControlTemplates;

public partial class ChicEntry : ContentView
{
    public static readonly BindableProperty TextProperty =
        BindableProperty.Create(nameof(Text), typeof(string), typeof(ChicEntry), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
        {
            var conttol = (ChicEntry)bindable;
            conttol.Text = newValue as string;
        });

    public static readonly BindableProperty PlaceholderProperty =
        BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(ChicEntry), string.Empty);

    /// <summary>
    /// The string to be edited.
    /// </summary>
    public string Text
    {
        get => (string)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }
    public string Placeholder
    {
        get => (string)GetValue(PlaceholderProperty);
        set => SetValue(PlaceholderProperty, value);
    }
    public ChicEntry()
    {
        InitializeComponent();
    }
}

Signup ViewModels

 public SignUpViewModel()
        {
            Title = AppResources.SignUp;
            SignUpCommand = new Command(SignUp);  
            // SignUpCommand = new Command(async () => await Shell.Current.GoToAsync($"../.."));
            LoginCommand = new Command(async () => await Shell.Current.GoToAsync(".."));
            //TermsCommand = new Command(async () => await Browser.OpenAsync("https://docs.microsoft.com/en-us/dotnet/maui/"));
        }

        private void SignUp(object obj)
        {
            var user = FulltName;
        }

the signup view

 <template:ChicEntry Text="{Binding Email, Mode=TwoWay}" 
                                    ControlTemplate="{StaticResource ChicEntryControlTemplate}"/>

and when I click on signup button the FullName will be null while I already have a value in entry field

Upvotes: 0

Views: 569

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 8080

Since you put the Entry control in the ControlTemplate and bind the Text Property to the BindableProperty of the ChicEntry, I recommend you use the TemplateBinding.

<Entry Text="{TemplateBinding Text, Mode=TwoWay}" 
       Placeholder="{Binding Placeholder, Mode=TwoWay}" 
       ...
       />

When enter some input in the Entry, the BindableProperty which Text binds to will also change, and the Property in ViewModel will also change.

And you don't need to register the property-changed callback method,

        //var conttol = (ChicEntry)bindable;
        //conttol.Text = newValue as string;

For more info, please refer to Pass parameters with TemplateBinding.

Upvotes: 0

Related Questions