1

I would like to create viewmodel properties in runtime.

I'm not so familiar with MVVM in UWP. Rather windows forms. In the past I created custom class object with reflection and I had possibility to add properties in runtime. In current project I prepared solution with mvvm ligt and UWP app. Works fine with data exchange on viewmodel level. Now I try to find how to create properties of viewmodel in runtime ie. from descriptions in xml file.

namespace hmi_panel.ViewModels
{
    public class HomeViewModel : ViewModelBase
    {
        #region Fields
        readonly IPlcService _plcService;
        #endregion

        #region Constructors

        public HomeViewModel(IPlcService dummyPlcService)
        {
            _plcService = dummyPlcService;
            _plcService.Connect("127.0.0.1", 0, 1);

            //zdarzenie cyklicznego odswiezania zeminnych
            OnPlcServiceValuesRefreshed(null, null);
            _plcService.ValuesRefreshed += OnPlcServiceValuesRefreshed;
        }

        #endregion

        #region Properties

        public string AppVersion
        {
            get { return $"{Package.Current.Id.Version.Major}. 
{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Build}. 
{Package.Current.Id.Version.Revision}"; }
        }

        public string AppCopyright
        {
            get { return "plc service: " + _plcService.ConnectionState.ToString(); }
        }

        private bool _pumpState;
        public bool pumpState
        {
            get { return _pumpState; }
            set {

                 _pumpState=value;
                RaisePropertyChanged(() => pumpState);

            }
        }

        #endregion

        #region Methods
        private RelayCommand _ConnectCommand;
        public RelayCommand ConnectCommand
        {
            get
            {
                return _ConnectCommand ?? (_ConnectCommand = new RelayCommand(() =>
                {
                    pumpState = true;

                }, () => true));
            }
        }

        private void OnPlcServiceValuesRefreshed(object sender, EventArgs e)
        {

            pumpState = _plcService.PumpState;

        }

        #endregion
    }
}

Property pumpState value is readed and writed with _plService. I can change value and I can read after external change. I would like to start only with bidirectional binding in xaml and create needed property ie. pumpState when viewmodel instance is created ie. in construtor.

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
Jakub Okaj
  • 11
  • 1
  • I've helped you format your code. Please read [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) to format your post next time. – Xie Steven Jan 03 '19 at 02:32
  • I actually did not quite understand what you want to do. Did you want to use **TwoWay** bindings to change your property's value on XAML? – Xie Steven Jan 03 '19 at 02:33
  • Yes I did. Property value in viewmodel is changed in proper way. I just want to create properties in viewmodel not in code but in runtime ie. from some description list. – Jakub Okaj Jan 03 '19 at 20:31
  • You could bind it on XAML and set `Mode=TwoWay`. For example, you could bind the `CheckBox`'s IsChecked to `pumpState` property. `` Then, when the CheckBox checked or unchecked, the `pumpState` property's value will be changed. – Xie Steven Jan 04 '19 at 01:51
  • That's done. It works. My goal is to do not define property pumpState in static code but build viewmodel object properties in runtime. – Jakub Okaj Jan 04 '19 at 09:25
  • It's impossible. – Xie Steven Jan 08 '19 at 09:54

0 Answers0