简体   繁体   中英

Clear multiple textbox value in wpf

I want to clear all text box value when i pressed a button.I used this code that is fine work in winform but when i am trying to use this same code in wpf then error occured at this.Controls position.Here is the code.Please give me a solution.

foreach (Control c in this.Controls)                 
    if (c is TextBox) 
       (c as TextBox).Clear(); 

I recommend looking into the MVVM pattern for WPF to solve your question.

By binding a textbox and button in your view (XAML) to a view model (class) you can clear the textbox values directly in the button command. There are many good MVVM frameworks like: Cinch and MVVM light to get you started.

Here is a sample that uses Cinch, but what's important is:
1. TextBox in row 0 uses TwoWay binding to Text1
2. TextBox in row 1 uses TwoWay binding to Text2
3. Button in row 2 uses Command binding to Clearcommand that sets Text1 and Text2 to string.Empty

Here is the view:

<Window x:Class="TextboxClear.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF" 
    meffed:ViewModelLocator.ViewModel="MainWindowViewModel"            
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Text="{Binding Path=Text1, Mode=TwoWay}"/>
    <TextBox Grid.Row="1" Text="{Binding Path=Text2, Mode=TwoWay}"/>
    <Button Grid.Row="2" Content="Clear" Command="{Binding Path=ClearCommand}"/>
  </Grid>
</Window>

Here is the view model:

using System;
using System.ComponentModel.Composition;
using Cinch;
using MEFedMVVM.ViewModelLocator;

namespace TextboxClear.ViewModels
{
  [ExportViewModel("MainWindowViewModel")]
  [PartCreationPolicy(CreationPolicy.Shared)]
  public class MainWindowViewModel : ViewModelBase
  {
    [ImportingConstructor]
    public MainWindowViewModel()
    {
      ClearCommand = new SimpleCommand<Object, Object>(CanExecuteClearCommand, ExecuteClearCommand);
    } 

    private string _text1 = string.Empty;
    public string Text1
    {
      get
      {
        return _text1;
      }
      set
      {
        _text1 = value;
        NotifyPropertyChanged("Text1");
      }
    }  

    private string _text2 = string.Empty;
    public string Text2
    {
      get
      {
        return _text2;
      }
      set
      {
        _text2 = value;
        NotifyPropertyChanged("Text2");
      }
    }

    public SimpleCommand<Object, Object> ClearCommand { get; private set; }
    private void ExecuteClearCommand(Object args)
    {
      Text1 = string.Empty;
      Text2 = string.Empty;
    }

    private bool CanExecuteClearCommand(Object args)
    {
      return true;
    }
  }
}

Use VisualTreeHelper.GetChild(). For example, if your textboxes are inside a StackPanel called StackPanelNew, use

  for (int i = 0;i < VisualTreeHelper.GetChildrenCount(this.StackPanelNew);i++) {
    TextBox txt = VisualTreeHelper.GetChild(this.StackPanelNew, i) as TextBox;
    if (txt != null)
    {
      //do stuff
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM