1

I have a question, I am building small login system, basically its ready and working, but still having some problems with UI, si if I take such button click action

    private void LoadButton_Click(object sender, RoutedEventArgs e)
    {
        Nullable<bool> creditencialFile = _controls.CredencialsFileDialog.ShowDialog();
        if (creditencialFile == true)
        {
            ContextStatic.Filename = _controls.CredencialsFileDialog.FileName;
            FileInfo creditencialsFileInfo = new FileInfo(ContextStatic.Filename);
            ContextStatic.RootFolder = creditencialsFileInfo.DirectoryName;
            model.LeapCreditencials = CredentialHelper.LoadCredentials(ContextStatic.Filename);
        }
    }

It loads credentials from file, and they are saved in object attribute:

model.LeapCreditencials = CredentialHelper.LoadCredentials(ContextStatic.Filename);

Now i want to refresh or reload UI so I all information windows would be set up to with new info. Question is should I need to reload per one control, or there is a smart way to reload Ui with new object values?

2
  • 5
    Don't you use databinding? Commented Jun 20, 2016 at 9:47
  • 1
    If you are building a WPF application without using binding, then you would be better off using WinForms. And while you are onto binding, it's recommended to do it using MVVM too. Commented Jun 20, 2016 at 9:49

2 Answers 2

3

Yes, you should implement INotifyPropertyChanged in your model msdn description to implement INotify Interface

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.
When value of model is changed it will reflect in the UI.

Xaml

 <TextBox Text="{Binding Mymodel.CustomerName,
           UpdateSourceTrigger=PropertyChanged}" />

Model

public class DemoCustomer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {

                this.customerNameValue = value;
                NotifyPropertyChanged();
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Bindings should be used between view and viewmodel layer, not between view and model
There should be viewmodel between this, This is a concise of the answer
I thought it was confusing because you talk about "model" in the binding and in your description. Just wanted to point out that but your code will work
-1

Yeah, there's a smart way. It's called MVVM (a.k.a. Model View View Model). It is not so hard to understand. You just bind your view to values in ViewModel, and when a value is changed UI is automatically updated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.