1

I'm new to WPF and I'm attempting to incorporate MVVM design pattern into my projects. In all the MVVM examples I've seen, the MainWindow.xaml.cs is only used to set the DataContext to the view model.

this.DataContext = viewModel;

Everything is very neat and decoupled away from the UI. events were also replaced with commands. There are two questions that I have regarding this.

  1. I'm wondering about how you are supposed to hook up controls that don't have the command property.

  2. What am I supposed to do when I would typically interact directly with a control e.g. Perhaps I want to set a combobox's index to -1. How am I supposed to do this on the view model?

David Andrew Thorpe
  • 838
  • 1
  • 9
  • 23
  • 3
    Can you clarify exactly what "hook up control" means to you, in concrete programming terms? Your viewmodels will never reference controls. They'll never even know controls exist. What you'll do is bind ComboBox.ItemsSource to a collection property of your viewmodel, and you will bind ComboBox.SelectedItem to another property of your viewmodel. If you set the viewmodel's selected item property to null, the ComboBox will have no selection. ([SelectedValue](https://stackoverflow.com/a/4902454/424129) is another way to bind to a combobox selection, but the same principles apply) – 15ee8f99-57ff-4f92-890c-b56153 Jul 29 '19 at 14:24
  • 3
    It's all done by Bindings. If the control doesn't have a Command property that can be bound to an ICommand property in a view model, it can still have properties that are bound TwoWay and hence transport data back to the view model. – Clemens Jul 29 '19 at 14:24
  • 2
    The 1st question is too broad, which control are you actually having problems with? The 2nd question, you would bind the selectedvalue and set the property it was bound to. – Kevin Cook Jul 29 '19 at 14:25
  • If you use a control B as a ´DataContext´ for the control A, then is the control B view or viemodel? ;) – Rekshino Jul 29 '19 at 14:42
  • Like Clemens already said, it's done by bindings. You have typically two types of user input: data manipulation and actions. For data manipulations (changing values of properties) you use data binding: [Data Binding Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview). For actions (e.g. calculate result or start operations like a download) you use ICommands: [Commanding Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/commanding-overview). – BionicCode Jul 29 '19 at 14:43
  • @EdPlunkett I would rather say - VewModel and what I have wanted to say with my question is, that UI controls also can be a viewmodel, if they being used as data context. – Rekshino Jul 29 '19 at 14:53
  • 1
    @rekshino I agree with EdPlunkett, if you think a 'control' *is* a 'view model' then you don't really understand MVVM. – Neil Jul 29 '19 at 15:14
  • Thanks @EdPlunkett Binding to 'SelectedItem' to a property in my viewmodel totally makes sense. I'll include this in the answer – David Andrew Thorpe Jul 29 '19 at 15:24
  • @Clemens Thanks buddy, yes I'm so new to WPF I didn't even think about binding to an ICommand property. Cheers for the answer – David Andrew Thorpe Jul 29 '19 at 15:25
  • @BionicCode Thanks for reiterating the point of binding to ICommands, I hadn't thought of that. – David Andrew Thorpe Jul 29 '19 at 15:25

1 Answers1

1

The collected comments by @EdPlunkett, @Clemens and @BionicCode answered my questions.

To Summarise:

  1. I can interact with controls by binding to their properties through INotificationChanged and ObservableCollection

  2. Elements that don't have a command property can still have properties bound to an ICommand property in the viewmodel.

David Andrew Thorpe
  • 838
  • 1
  • 9
  • 23
  • 1
    I'll add one more point that should complement this answer - Behaviors. Behaviors are MVVM's solution to a control that doesn't have good binding support, like ListBox's lack of a bindable `SelectedItems` property (plural). A Behavior let you write view-side code in an isolated manner, hook it up to the naughty control, and manage your MVVM and binding via that behavior. – Avner Shahar-Kashtan Aug 04 '19 at 12:26
  • Thanks buddy I'll take a look into that – David Andrew Thorpe Aug 04 '19 at 12:29