0

I wonder how can I reload the UserControl to it's original template from inside,

<UserControl x:Name="_UserControl">
<!-- other input controls and combos -->
 <Button OnClick="Reload_UserControl"/>
</UserControl>

is there any this.reCreate() similar method?

Mhood
  • 1
  • 1
  • 1

2 Answers2

0

My Answer is based on a answer posted for Windows Forms 8+ years back.

I have used the same logic from that answer and created extension method on the Root Grid to loop through and clear values of each element( Mimicking it to bring the UserControl back to its usual state)

Below is the Extension Method.

public static class Extensions
{
    private static Dictionary<Type, Action<UIElement>> controldefaults = new Dictionary<Type, Action<UIElement>>()
    {
        {typeof(TextBox), c => ((TextBox)c).Text = String.Empty},
        {typeof(CheckBox), c => ((CheckBox)c).IsChecked = false},
        {typeof(ComboBox), c => ((ComboBox)c).SelectedIndex = 0},
        {typeof(ListBox), c => ((ListBox)c).Items.Clear()},
        {typeof(RadioButton), c => ((RadioButton)c).IsChecked = false},
    };

    private static void FindAndInvoke(Type type, UIElement control)
    {
        if (controldefaults.ContainsKey(type))
        {
            controldefaults[type].Invoke(control);
        }
    }

    public static void ClearControls(this UIElementCollection controls)
    {
        foreach (UIElement control in controls)
        {
            FindAndInvoke(control.GetType(), control);
        }
    }

    public static void ClearControls<T>(this UIElementCollection controls) where T : class
    {
        if (!controldefaults.ContainsKey(typeof(T))) return;

        foreach (UIElement control in controls)
        {
            if (control.GetType().Equals(typeof(T)))
            {
                FindAndInvoke(typeof(T), control);
            }
        }
    }
}

For usage, Give the RootGrid Some name. Say rootGrid. So all you need to call is

rootGrid.Children.ClearControls();

Also As mentioned in other answer, if you want to clear only specific controls, you can use

rootGrid.Children.ClearControls<TextBox>();

You can find the Extensions class on my Gist

Good Luck.

Community
  • 1
  • 1
AVK
  • 3,893
  • 1
  • 22
  • 31
  • thank you for your suggestion, unfortunately this can't be applied to a dynamic custom usercontrols, some controls have their own data, the method I'm looking for should be like creating anew instance of the custom usercontrol or ask the parent to remove it then add a new one with same name. – Mhood Apr 27 '17 at 16:43
0

If what you mean is clearing the values entered by a user in various fields, then using MVVM makes it quite simple - you can either replace the view model with a new one and the bindings would update everything. You could also have a Clear method in the view model.

If what you mean is fixing the topology of the visual tree that might have been altered from the original - your best bet would be to replace your control with a new one.

Also worth noting - "template" is usually not a word associated with user controls. User controls define their visual tree in XAML, which is loaded every time the control is created. Templated controls use a template that is typically loaded once and applied to each control when it loads. See When to use a templated control over a UserControl? for some more potentially useful info.

Community
  • 1
  • 1
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • I'm thinking of replacing my UserControl with a new one, or remove this control from it's parent and recreate it again with the same name. the issue is just to create the "Cancel" button, I don't wanna loop over the controls and empty them, some controls are already custom usercontrols. – Mhood Apr 27 '17 at 16:47
  • You should be using MVVM and then your data object could easily be replaced with a new empty one. – Filip Skakun Apr 27 '17 at 18:08