1

In part of a XAML code maximum and minimum values are set as follows:

<WindowsFormsHost>
    <wf:NumericUpDown  Maximum="12000" Minimum="120" x:Name="MyNumericUpDown" TextAlign="Right"/>
</WindowsFormsHost>

And in the same C# program inside a class I have the following:

namespace MyNameSpace
{
    public class MyClass
    {
        public int max { get; } = 12000;
        public int min { get; } = 120;

        ...
    }
}

Is it possible to set the Maximum and Minimum values of NumericUpDown by using the class properties instead of hardcoding them? So that of I change class property values the XAML values autonomically updated.

MindSwipe
  • 7,193
  • 24
  • 47
user1999
  • 199
  • 8

1 Answers1

0

Yup, it's possible, and even what I'd consider to be fundamental in WPF development. It's called data binding. I'll be implementing a simple one way data binding here which binds to a property on the code behind file (i.e MainView.xaml binds to MainView.xaml.cs), but you can bind to any class you'd like, this then forms the fundamentals of MVVM.

Xaml:

<Window x:Class="MainView"
    Title="MainView"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <!-- Snip -->
</Window>

The important part is DataContext="{Binding RelativeSource={RelativeSource Self}}. Right here we're telling the window to bind to itself (i.e the code behind file). This is required for anything to work. You can use this Q&A to see how to bind to something else.

Continuing on:

<wf:NumericUpDown Maximum="{Binding Max}" Minimum="{Binding Min}" />

Here we're telling the NumericUpDown component to get its Maximum and Minimum values from the data context. Now, one caveat, you can only bind to properties and nothing else. So, if you ever run into a problem where your bindings aren't working, check to see if you're binding to a property or not. Here's the code of code behind file

public class MainWindow
{
    public int Max { get; } = 12000;

    public int Min { get; } = 120;

    public MainWindow()
    {
        InitializeComponent();
    }
}

This is a one way data binding, i.e the XAML can only read these values. For things like an input box, you'll need to use TwoWay bindings. And if anything changes those values (currently impossible, but may be in the future), then you need to implement INotifyPropertyChanged


P.S: No guarantees for the code. I just typed it out in this text box without any auto complete and the last time I did WPF is quite a while ago. Just leave a comment if something doesn't work and I'll try my best to fix it

MindSwipe
  • 7,193
  • 24
  • 47
  • Be aware that OP already has a "view model" class. Using the Window instance as DataContext makes no sense. There should instead be `DataContext = myClass`, with myClass being an instance of `MyClass`. – Clemens Apr 19 '21 at 08:53
  • @Clemens we have no idea where `Min` and `Max` are declared, because `MyNamespace` and `MyClass` are obviously placeholders, they might as well be declared on the code behind for all we know. And if they're not, I linked to a StackOverflow Q&A which explains how to set a class as data context which is not the code behind file. – MindSwipe Apr 19 '21 at 09:36
  • "*In the same C# program inside a class I have the following*" - they are obviously declared in MyClass, as shown in the question. If an actual class name is *potentially* different, OP could well handle that. – Clemens Apr 19 '21 at 09:46