Link to home
Start Free TrialLog in
Avatar of auldh
auldh

asked on

MVVMLight C# RaisePropertyChanged not working

I had assistance from StackOverflow to help me get my content change on a button however I'm seeing not change until my working class called '''FFC.StartCollection()''' is complete. So the user does not get any status of what is happing. I want the button to change from enabled to disabled and the content to say "working"followed by "done".

The BtnEnabled = true; BtnContent = "Done"; only seem to change when the button command is complete. No change to the values before the function FFC.StartCollection() is ran.

My code (I call working class) that does all the heavy lifting runs for about 2 minutes when '''FFC.StartCollection();''' is called. But what I see is that once the button is clicked none of the button changes take affect. So the user does not see the button go into disable and the content change to "working.."

I stepped through the code. I'm seeing the RaisePropertyChanged actually being pass the message I want. However the GUI never shows it. In Windows Form (right or wrong) I could do a ".Refresh()" to have the GUI updated.

using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Threading;

/// <summary>
/// just so i remember the TextBox will not work in this function. ListBox work for 
/// updating!
/// </summary>

namespace WpfFileFolderTool.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainWindowViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        private BindingList<string> _fileCollectionItems;//can't be update but via property below
        public ObservableCollection<string> FileCollection { get; set; }
        public RelayCommand AddListCommand { get; set; }
        public RelayCommand BeginCollectionCommand { get; set; }
        public BindingList<string> FileCollectionItems 
        { 
            get => _fileCollectionItems;
            set
            {
                _fileCollectionItems = value;
                RaisePropertyChanged(nameof(FileCollectionItems));
            }
        }
        bool m_Enabled = true;
        public bool BtnEnabled
        {
            get { return m_Enabled; }
            set
            {
                m_Enabled = value;
                //RaisePropertyChanged("BtnEnabled");
                RaisePropertyChanged(()=>BtnEnabled);
            }
        }
        string m_String = "Start";
        public string BtnContent
        {
            get { return m_String; }
            set
            {
                m_String = value;
                //RaisePropertyChanged("BtnContent");
                RaisePropertyChanged("BtnContent");
                //RaisePropertyChanged(() => BtnContent);
            }
        }
        public MainWindowViewModel()
        {
            //FileCollection = new ObservableCollection<string> { "Another Item" };
            FileCollectionItems = new BindingList<string>();

            BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);

        }
        //addcollection writes the string to textbox
        public void BeginCollectionCommandExecute()
        {
            BtnEnabled = false;
            BtnContent = "Working";

            FileFolderCollection FFC = new FileFolderCollection();
            FFC.StartCollection();

            //string item = "Count = " + Count.ToString();
            //FileCollectionItems.Add(item);
            //Count++;

            BtnEnabled = true;
            BtnContent = "Done";
            Thread.Sleep(5000);
            BtnContent = "Start";
        }


        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
} ```

Open in new window


I'm new to WPF/MVVMLight programming. I thought it was time to jump in.
However I'm have issues with the GUI updates.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.