如何在内容模板中的Xamarin表单中访问条目TextBox值

问题描述 投票:0回答:1

我有一个Xamarin Form XAML,我想重置绑定条目文本框的值:

<ContentView Grid.Row="2"  Margin="30,0,30,0" BackgroundColor="White" Padding="1">
                        <Entry 
                            x:Name="txtLastName" 
                        Text="{TemplateBinding BindingContext.ContactDetails.LastName}" 
                        Placeholder="Last Name" PlaceholderColor="#cccccc" HeightRequest="40" 
                        TextColor="Black" FontSize="Medium"
                        VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
                    </ContentView>

如果我想执行某项操作(例如将其中的值设置为空,是否可以通过后面的代码(例如通过单击按钮)使Entry内的值访问)

  private void ClearTextBoxesButtonClicked(object sender, EventArgs e)
{
            //Clear value
            this.FindByName<Entry>("txtLastName").Text = string.Empty;

}

执行上述操作将导致文本框为空值,即使以表单形式填充该字段也不会填充该值。似乎找不到文本框。

非常感谢任何指针。

.net xaml xamarin xamarin.forms xamarin.android
1个回答
0
投票

根据Jason的回复,输入文本绑定到BindingContext.ContactDetails.LastName,如果要清除输入文本值,只需清除ContactDetails.LastName。

注意:请不要忘记为ContactDetails类实现INotifyPropertyChanged接口。

请看下面的代码:

<ContentPage.Resources>
    <ControlTemplate x:Key="template">
        <ContentView>
            <StackLayout>
                <Entry
                    x:Name="txtLastName"
                    FontSize="Medium"
                    HeightRequest="40"
                    HorizontalOptions="FillAndExpand"
                    Placeholder="Last Name"
                    PlaceholderColor="#cccccc"
                    Text="{TemplateBinding BindingContext.contact.LastName}"
                    TextColor="Black"
                    VerticalOptions="End" />
            </StackLayout>
        </ContentView>
    </ControlTemplate>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <ContentView ControlTemplate="{StaticResource template}" />
        <Button
            x:Name="btn1"
            Clicked="btn1_Clicked"
            Text="btn1" />
    </StackLayout>

</ContentPage.Content>

  public partial class Page18 : ContentPage
{
    public ContactDetails contact { get; set; }
    public Page18()
    {
        InitializeComponent();
        contact = new ContactDetails();

        contact.LastName = "cherry bu";
        this.BindingContext = this;

    }

    private void btn1_Clicked(object sender, EventArgs e)
    {
        contact.LastName = "";
    }
}

public class ContactDetails:ViewModelBase
{
    private string _LastName;
    public string LastName
    {
        get { return _LastName; }
        set
        {
            _LastName = value;
            RaisePropertyChanged("LastName");
        }
    }

}

ViewModelBase是实现INotifyPropertyChanged的类

 public class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.