1

Hi i am famliar with windows forms, Now i am going to start WPF, in windows i reset the controls with the following recursive method

 internal  void clrCntrls(Control cntrl)
    {
        if (cntrl.GetType() == typeof(TextBox))
        {
            TextBox cntrl = (TextBox)cntrl;
            cntrl.Text = "";               
        }            
         else if (cntrl.GetType() == typeof(ComboBox))
        {
            ComboBox cntrl = (ComboBox)cntrl;
            cntrl.SelectedIndex = -1;
        }
        else
        {
            foreach (Control subCntrl in _cntrl.Controls)
            {
                clrCntrls(subCntrl);
            }
        }

it works fine for me in windows but the same how can i do in WPF. i am slight Confusion with that. Please help with it.

Pallavi
  • 87
  • 1
  • 13

2 Answers2

4

I am famliar with windows forms, Now i am going to start WPF

Excellent. Welcome to the light side.

The first thing you need to do is to completely forget everything you've learned in winforms and understand and embrace The WPF Mentality.

in winforms i reset the controls with the following recursive method

in WPF, you don't "reset controls", you don't actually do much with controls, simply because UI is Not Data.

What you do instead, is to declaratively DataBind your UI to a relevant Data Model or ViewModel, and manipulate that instead.

Therefore, say define some data like:

public class Person
{
    public string FirstName {get;set;}

    public string LastName {get;set;}
}

and then you define (IN XAML) a piece of UI to show that data:

  <StackPanel>
      <TextBlock Text="{Binding LastName}"/>
      <TextBlock Text="{Binding FirstName}"/>
   </StackPanel>

What you do in order to "clear" these TextBoxes is to assign their DataContext to a new, clear instance of your data class:

DataContext = new Person();

I suggest you start reading the WPF Mentality link.

Good luck.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • 2
    +1 excellent write up. I am sure @HighCore didn't want to clutter the answer, because there is a lot to cover in WPF, but for bindings to respond, don't forget for the class, in this sample `Person`, to implement INotifyPropertyChanged and raise the PropertyChanged event on properties that are being used in XAML Bindings. – denis morozov Jan 03 '14 at 19:25
  • 2
    @Denismorozov that's right. There's much more to WPF than this, but all the links I posted should get the OP started. – Federico Berasategui Jan 03 '14 at 19:26
2

You could try using VisualTreeHelper, but it'll only work for controls that are currently in the visual tree, i.e. rendered. If you have a TabControl you won't get access to controls on a TabItem that is not selected at the moment.

Here's a quick attempt at it:

private void ClearControls(DependencyObject root)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
    {
        var control = VisualTreeHelper.GetChild(root, i);

        if (control is TextBox)
        {
            (control as TextBox).Text = String.Empty;
        }
        else if (control is ComboBox)
        {
            (control as ComboBox).Text = String.Empty;
        }
        else if (VisualTreeHelper.GetChildrenCount(control) > 0)
        {
            ClearControls(control);
        }
    }
}

My suggestion would be to take advantage of the MVVM pattern instead. In this case you will have the control contents bound to properties in a view model class. To clear all controls in this case, you will only have to replace DataContext with a new instance of the view model class. Check HighCore's answer for details.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • 1
    @Pallavi It is important to mention that this approach has **ALL SORTS OF PROBLEMS** due to `UI Virtualization` and the WPF Content Model and the way a TabControl creates and destroys UI elements dynamically and Visual versus Logical Trees. DO NOT use this approach. WPF is NOT winforms, see my answer. – Federico Berasategui Jan 03 '14 at 19:31
  • what is root here. tabitem – Pallavi Jan 03 '14 at 19:32
  • 1
    @Pallavi I'd also like to reiterate what @HighCore has said. This approach is going to work only in very simple scenarios where the visual tree doesn't change during runtime (i.e. no conrtols derived from `ItemsControl`). If you are going to use this, do it only as an **interim solution** and consider changing your approach to proper MVVM model ASAP. – Damir Arh Jan 04 '14 at 08:37
  • @Pallavi `root` should be your window or any other top level container control, containing all the child controls you want to "clear". – Damir Arh Jan 04 '14 at 08:39