0

Please help me

I have

public partial class OrderControl : UserControl
{
    private OrderHeader orderHeader;
    public Customer selectedCustomer { get; set; }
    private Customer[] allCustomers;
    public User selectedManager { get; set; }
    private User[] allManagers;


    public OrderControl()
    {
        InitializeComponent();
        DataContext = this;
    }
...
}

And I need one way binding to source:

<ComboBox Name="CustomerComboBox" SelectedItem="{Binding selectedCustomer}"/>

Is this best way to keep selectedCustomer Property in OrderControl.xaml.cs or I need to create some OrderViewModel class with ..,selectedCustomer,... Properties and keep an instance of OrderViewModel in OrderControl.xaml.cs?

thanks

H.B.
  • 166,899
  • 29
  • 327
  • 400
artos
  • 751
  • 1
  • 10
  • 25

3 Answers3

0

It is best to create a ViewModel class, move your properties to that class and make it a DataContext of your UserControl.

Also, your selectedCustomer property is just a regular .NET property and it needs to support INotifyPropertyChanged interface in order to facilitate binding and change notification... typically a base ViewModel class from which all your other ViewModel classes inherit would implement this interface...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • The property must not be an DependencyProperty it is ok if he puts it in an ViewModel to bind it that way to his ComboBox. What he needs to implement is INotifyPropertyChanged to provide notification. – Mark Jul 19 '12 at 18:08
  • @Mark, you're right it doesn't need to be DP... typically a ViewModel base class implements INotifyPropertyChanged... – Dean Kuga Jul 19 '12 at 18:29
  • But if you assign the UsersControl's viewmodel to its own DataContext then that breaks bindings made in a parent control that composes the UserControl. Specifically bindings made in the owning parent control will then resolve to the UserControl's own viewmodel instead of the parent's viewmodel, which is unexpected and requires messy workarounds. – Neutrino Jan 10 '13 at 11:01
  • @Neutrino You're wrong, the parent has its own VM and a UC whose DataContext is some other VM is in no way going to interfere with that... – Dean Kuga Jan 10 '13 at 17:26
  • If the UserControl binds its own DataContext it does interfere with the parent's bindings http://stackoverflow.com/questions/12819044/usercontrol-using-wrong-datacontext which surprised the hell out of me too. – Neutrino Jan 11 '13 at 10:15
0

That will work if you implement INotifyPropertyChanged. Right now there is no way for the combobox to get updates when the property is set. See http://msdn.microsoft.com/en-us/library/ms229614.aspx

However, if you wish to follow MVVM, then you will want to create a view model object.

Bort
  • 817
  • 6
  • 22
  • Why would OP need to implement INotifyPropertyChanged since all OP wants is one-way binding to source. I agree, if OP wanted OneWay or TwoWay binding, INotifyPropertyChanged would have to be implemented, but not in this case. – Thelonias Jul 19 '12 at 18:54
0

if you wanna create real usercontrols you should not:

 DataContext = this;

here a quote from H.B.

It's bad practice, setting the DataContext like that is invisible "from the outside" and impractical as inheritance of the DataContext is usually what you want and expect

here is are similar question and answer.

but if you wanna do MVVM with viewmodel first.

quote from Rachel:

Remember, with MVVM your ViewModels are your application. The View is just a pretty interface that allows users to interact with your ViewModels.

that mean you should create appropriate viewmodels with all properties and commands you need. remove all code from your usercontrol because its now just a view. viewmodel first connects the viewmodel and the view through datatemplates.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74