简体   繁体   中英

Looping with Wpf tabitem Controls using C#?

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.

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.

You could try using VisualTreeHelper , but it'll only work for controls that are currently in the visual tree, ie 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.

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