0

I create (if not exists) a new ViewModel Instance via the IMessenger (MVVM Light Toolkit) and pass a custom Object trough the constructor, which is a Property of my MainViewModel. In this ViewModel I set it to a Property too and using it e.g. for a Command Execute Method. But when I trigger the Command via Binding, the custom Object Property looses its values. I already debugged and saw, that the object get's passed correctly with its values, but after initializing from the constructor its empty (Not null, just empty Properties).

My custom Object

public class CustomObject : ObservableObject
{
    public int Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    // etc...
}

I create the ViewModel like this

public CustomObject CustomObj
{
    get { return _customObj; }
    set { Set(ref _customObj, value); }
}

_customViewModel = new CustomViewModel(CustomObj, _dataService);

The ViewModel

public CustomViewModel(CustomObject obj, IDataService dataService)
{
    _dataService = dataService;

    // Here it sets correctly the Object
    CustomObj = obj;
}

public CustomObject CustomObj
{
    get { return _customObj; }
    set { Set(ref _customObj, value); }
}

// Even before the Command is triggered, the Object is already empty
public ICommand SomeCommand => new RelayCommand<string>(async s =>
{
    var someThing = await _dataService.GetSomeData(CustomObj.Id);

    // Stuff...
}

They are registered in the SimpleIoC Container as well, if that matters.

What could result that?

Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
julianstark999
  • 3,450
  • 1
  • 27
  • 41

0 Answers0