【问题标题】:How to Access Entry TextBox value in Xamarin Forms within a Content Template如何在内容模板中访问 Xamarin 表单中的条目文本框值
【发布时间】:2020-09-11 03:15:20
【问题描述】:

我有一个 Xamarin 表单 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>

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

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

}

执行上述操作会导致文本框为空值,即使该字段已填充到表单中,也不会填充该值。好像找不到文本框。

任何指针都非常感谢。

【问题讨论】:

  • 如果你使用绑定,那么你通过清除它绑定到的属性来清除文本框
  • 正如 Jason 所说,您可以将 ContactDetails.LastName 的值设置为 string.Empty 或 null,这将重置您的 Entry 的绑定文本属性的值。

标签: .net xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

根据Jason的回复,entry text绑定BindingContext.ContactDetails.LastName,如果要清除Entry Text值,只需要清除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));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2017-03-26
    • 2023-04-03
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多