0

There are default values you can set for some of window properties in WPF. For example you can set a Canvas' Width or a TextBlock's Text in XAML and then your app can change them by accessing the controls. Are there any ways of setting those values to how they were declared in XAML without saving them yourself? Are they kept somewhere so that you can access them at runtime?

  • 3
    How does hiding a window save memory? – Hintham Oct 30 '17 at 08:19
  • 1
    You don't need it. Just bind to a new DTO/ViewModel. WPF (and Windows Forms) applications use databinding to modify the UI controls, *not* direct manipulation of the controls. Create one class to hold all the data and bind different controls to different properties. If you want to change the entire form at once, all you need is to replace the original instance and raise the INotifyPropertyChanged event – Panagiotis Kanavos Oct 30 '17 at 08:19
  • 1
    BTW that's what the V and VM stand for in MVVM. V is your form, VM the class instance that contains the data for this form. All you need is set that instance as the `DataSource` of the entire form, and bind individual control properties to it. You could use the same instance for multiple different forms, or even different panels, eg a list of stock quotes appearing as a grid in one view, chart in another – Panagiotis Kanavos Oct 30 '17 at 08:21
  • @Hintham My windows execute pretty heavy code on creation which allocates a lot of memory, but it only needs to be done once. I could do checks inside the code but this works too. –  Oct 30 '17 at 08:33
  • 2
    @ALazyDoe then your window is trying to do the job of a service or other kind of backend code. It's impossible to help more without the code, but a *view* should have to do *anything* other than display the data. You can avoid allocations by cleaning up the code too, eg avoid temporary strings or avoid loading more data than you really need. – Panagiotis Kanavos Oct 30 '17 at 09:01
  • I worded that wrong, it's not the window that executes the code, it's just that the code is executed whenever a new window is opened. It's not executed by window events. –  Oct 30 '17 at 10:03

2 Answers2

0

I think that there is no way for WPF to know what your default values are but you can create your own attribute that contains information about default value and create method that will set that value.

First we need attribute that will hold information about default value of given property:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
class DefaultAttribute : Attribute
{
    public object Value;
    public DefaultAttribute(object value)
    {
        this.Value = value;
    }
}

Given this attribute we can easy declare default values for our properties like this:

class ViewModel
{
    [Default("Noname")]
    public string Name { get; set; }
    public int Age { get; set; }
}

Then if you want to apply defaults to your view model you need some kind of method that will do that for you:

private static void ApplyDefaults(object model)
    {
        PropertyInfo[] properties = model.GetType().GetProperties();

        foreach (var property in properties)
        {
            DefaultAttribute defaultAttr = property.GetCustomAttribute<DefaultAttribute>();

            if (defaultAttr != null)
                property.SetValue(model, defaultAttr.Value);
        }
    }

Example program with usage:

 public static void Main()
    {
        var vm = new ViewModel();
        Console.WriteLine($"Start - {vm}");
        ApplyDefaults(vm);
        Console.WriteLine($"ApplyDefaults - {vm}");
        vm.Name = "Damian";
        vm.Age = 23;
        Console.WriteLine($"After using setters - {vm}");
        ApplyDefaults(vm);
        Console.WriteLine($"After ApplyDefaults - {vm}");
    }

Output for the program is:

Start - - 0
ApplyDefaults - Noname - 0
After using setters - Damian - 23
After ApplyDefaults - Noname - 23

I've just checked that .NET have DefaultValue Attribute which you can use in similar way.

Shoter
  • 976
  • 11
  • 23
0

My question is if there is maybe a function or some hacky way of setting all the values to default somewhere in the wpf api?

No, there isn't.

You better close the window and create a new one. Or you will have to "manually" reset all values by writing your own custom code that does this.

The framework cannot be supposed to know when you want to reset the values of your control properties without you specifying it somewhere. And the way to specify it is to write code.

mm8
  • 163,881
  • 10
  • 57
  • 88