0

Binding works without INotifyPropertyChanged, why?

Is it a bad practice to bind my UI to HTTP API model directly without creating a separate view model?

The only disadvantage is it won't implement INotifyPropertyChanged so UI won't update when you change the property from your code - but what if you don't need to update your property from your code?

This is my API response model:

public class EventResponseModel { public int Id { get; set; } public string Subject { get; set; } public string Description { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public string ArgbColor { get; set; } // should bind to Brush public Location Location { get; set; } public IEnumerable<Person> Attendees { get; set; } } 

In the model above I have ArgbColor that need to be bound to Brush in XAML. I could probably create StringToBrushConverter : IValueConverter to make it work instead of creating a separate view model.

Is this always preferred to create a separate view model, map to it and then bind to it?

For editing, I would still have a separate VM and use a composition like:

public class EventVM { public EventResponseModel EditedEvent { get; set; } // available locations etc., commands for saving } 
5
  • 1
    "Is it a bad practice?" - no, not in my opinion. I would however recommend public Color ArgbColor { get; set; } and bind a SolidColorBrush's Color property. Commented Jun 13, 2019 at 9:14
  • Color needs a reference to System.Media or PresentationCore. This model is used on the server so string is the best type. Commented Jun 13, 2019 at 9:21
  • Does ArgbColor Value contains html clr codes ? like this( #1A85CD )? Commented Jun 13, 2019 at 10:46
  • @AvinashReddy yes, #AARRGGBB. It's not a problem to convert it using BrushConverter Commented Jun 13, 2019 at 10:53
  • Then why cant u just bind the string to color i mean Background="{Binding ArgbColor }" Like this. Commented Jun 13, 2019 at 10:57

1 Answer 1

1

Is it a bad practice to bind my UI to HTTP API model directly without creating a separate view model?

Not if you are and will be able to use the model class exactly as-is in your GUI application. Then there is no reason to create a view model wrapper just for the sake of it, since you basically have a dependency upon the API anyway.

Is this always preferred to create a separate view model, map to it and then bind to it?

No, not always. If you for example were to display a couple of hundreds or thousands of event responses in a grid of some kind, you don't want to invoke a converter for each item. In this case it would be better to wrap the model and bind directly to a UI friendly Brush property or similar. So it all depends on your requirements. But binding directly to domain objects isn't always a bad idea.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.