开发者

How to handle ViewModel and Database in C#/WPF/MVVM App

I have a task management program with a "Urgency" field. Valid values are Int16 currently mapped to 1 (High), 2 (Medium), 3 (Low), 4 (None) and 99 (Closed). The urgency field is used to rank tasks as well as alter the look of the items in the list and detail view.

When a user is editing or adding a new task they select or view the urgency in a ComboBox. A Converter p开发者_运维知识库asses Strings to replace the Ints. The urgency collection is so simple I did not make it a table in the database, instead it is a, ObservableCollection(Int16) that is populated by a method.

Since the same screen may be used to view a closed task the "Closed" urgency must be in the ItemsSource but I do not want the user to be able to select it. In order to prevent the user from being able to select that item in the ComboBox but still be able to see it if the item in the database has that value should I...

  1. Manually disable the item in the ComboBox in code or Xaml (I doubt it)
  2. Change the Urgency collection from an Int16 to an Object with a Selectable Property that the isEnabled property of the ComboBoxItem Binds to.
  3. Do as in 2 but also separate the urgency information into its own table in the database with a foreign key in the Tasks table
  4. None of the above (I suspect this is the correct answer)

I ask this because this is a learning project (My first real WPF and first ever MVVM project). I know there is rarely one Right way to do something but I want to make sure I am learning in a reasonable manner since it if far harder to Unlearn bad habits

Thanks

Mike


I would favor option 2. Sounds very MVVM-stylish to me.

Option 3 would be favorable, when there are other applications or when you have reports accessing the "Urgency" field. Reason: Otherwise you will need to duplicate the knowledge of mapping between Int16 and their meaning. Move the knowledge to the database to keep it in one place.

Maybe consider Enums to make the code more expressive:

enum Urgency { High=1, Medium=2, Low=3, Closed=99 };

This way you will have something nice looking for evaluating the IsEnabled property like this:

if (urgency == Urgency.Closed) return false;

When you need to store the numeric value of the enum, you will need to make a cast to Int16 beforehand.


I think that I'd first fix this in the view. Have a TextBlock that displays "Closed", and a ComboBox that displays the other values, and then use a data trigger to set IsVisible on both depending on whether or not Urgency is 99.

I'd do this not because it's the best technical solution (it's probably not) but because it's (possibly) the best UI solution. If the user can't ever modify a closed item, it's a little misleading to display "Closed" even in a disabled ComboBox, since the ComboBox means, visually, "Here's something you can change." That it's disabled just prompts the user to wonder what he has to do to enable it. Using a TextBlock is an unambiguous way of saying "this is just how it is."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜