Create property dynamically in viewmodel MVVM light and UWP












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.










share|improve this question

























  • I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

    – Xavier Xie - MSFT
    Jan 3 at 2: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?

    – Xavier Xie - MSFT
    Jan 3 at 2: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 3 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. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

    – Xavier Xie - MSFT
    Jan 4 at 1: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 4 at 9:25
















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.










share|improve this question

























  • I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

    – Xavier Xie - MSFT
    Jan 3 at 2: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?

    – Xavier Xie - MSFT
    Jan 3 at 2: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 3 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. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

    – Xavier Xie - MSFT
    Jan 4 at 1: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 4 at 9:25














1












1








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.










share|improve this question
















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.







c# mvvm uwp binding dynamicobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 2:22









Xavier Xie - MSFT

5,8041317




5,8041317










asked Jan 2 at 10:52









Jakub OkajJakub Okaj

61




61













  • I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

    – Xavier Xie - MSFT
    Jan 3 at 2: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?

    – Xavier Xie - MSFT
    Jan 3 at 2: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 3 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. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

    – Xavier Xie - MSFT
    Jan 4 at 1: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 4 at 9:25



















  • I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

    – Xavier Xie - MSFT
    Jan 3 at 2: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?

    – Xavier Xie - MSFT
    Jan 3 at 2: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 3 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. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

    – Xavier Xie - MSFT
    Jan 4 at 1: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 4 at 9:25

















I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

– Xavier Xie - MSFT
Jan 3 at 2:32





I've helped you format your code. Please read How do I format my posts using Markdown or HTML? to format your post next time.

– Xavier Xie - MSFT
Jan 3 at 2: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?

– Xavier Xie - MSFT
Jan 3 at 2:33





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?

– Xavier Xie - MSFT
Jan 3 at 2: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 3 at 20:31





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 3 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. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

– Xavier Xie - MSFT
Jan 4 at 1:51





You could bind it on XAML and set Mode=TwoWay. For example, you could bind the CheckBox's IsChecked to pumpState property. <CheckBox IsChecked="{Binding pumpState,Mode=TwoWay}" Content="descript"></CheckBox> Then, when the CheckBox checked or unchecked, the pumpState property's value will be changed.

– Xavier Xie - MSFT
Jan 4 at 1: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 4 at 9:25





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 4 at 9:25












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005025%2fcreate-property-dynamically-in-viewmodel-mvvm-light-and-uwp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005025%2fcreate-property-dynamically-in-viewmodel-mvvm-light-and-uwp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter