2

I'm creating a WPF application and at the top of the page I need to display a postal address. Underneath the postal address will be product information. The product information is returned from a datasource and bound to the datacontext. eg this.DataContext = myComplexType;

Staff need to manually switch which postal address is to be displayed. I think the best way would be to select would be via a radio button control. In other words, on my Page, I would have 3 radio buttons, *UK Address *USA Address *China Address and depending on which was selected, the appropriate text would be entered in the Textblock Name="txbPostalAddress" at the top of the page.

The postal addresses live within a class called Addresses (as strings). e.g. Code:

namespace DAL
{
    public class Addresses
    {
        public string GctHkAddress { get { return gctHkAddress;} }
        public string GctUsaAddress { get { return gctUsaAddress; } }
        public string GctUkAddress { get{return gctUkAddress;} }

        private string gctHkAddress = "Company Name\n Hong Kong \n";
        private string gctUsaAddress = "Company Name\n USA \n";
        private string gctUkAddress = "Company Name\n UK \n";
    }
}

My question is, should the binding be done in XAML or in code behind. I can do it in code behind quite easily but I get the feeling this negates the power of XAML. Does any one have any idea which is the better approach and if via XAML, any idea how or links to tutorials etc?

Thanks

Dave

vyegorov
  • 21,787
  • 7
  • 59
  • 73
Dave
  • 8,163
  • 11
  • 67
  • 103

3 Answers3

3

If your question is where to set DataContext, it just depends sometimes, otherwise it does not matter.

Usually ViewModel ( in short a class that has data, and commands), is set to DataContext.

It can be done in following ways

  1. In XAML -> Create staticresource for ViewModel in XAML, and set using StaticResoure. Issue -> View has to know about ViewModel, ViewModel must have parameterless constructor

  2. In ViewModel -> Pass view to constructor of View Model, and set view.DataContext=this in ViewModel ctor Issue -> ViewModel has to know about View

  3. Attach outside View and ViewModel, this is usually done in custom bootstrap class (or by overriding App-> OnStartUp. Here View is instanciated, ViewModel is instanciated, View.DataContext is set to ViewModel Issue -> Custom initialization is required

  4. ViewModelLocator -> create ViewModelLocator instance in XAML as StaticResource, bind DataContext to Property of ViewModelLocator Advantage -> view, viewmodel remain loosely coupled.

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • My DataContext is already bound to the page data. It's almost as if I need 2 datacontexts. One for the product data (which is bound already to a model), and another to the radio button! – Dave May 10 '12 at 14:12
  • you cannot attach 2 DataContext. All data bound properties that you need should be present in 1 DataContext. It is usually known as ViewModel because it suffice all the properties and command needed by View – Tilak May 10 '12 at 14:14
  • Ah... So I can just update my object so it brings all the models! Thank you – Dave May 10 '12 at 14:18
0

If you plan to use MVVM then XAML is best option else you can do it in code behind as well

Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • Yeah, it is MVVM. Do you know of any tutorials or any example code? I did Google it but must be entering the wrong search terms. – Dave May 10 '12 at 14:10
  • Here are some :- http://www.codeproject.com/Articles/221585/Step-By-Step-Guide-To-MVVM http://www.codeproject.com/Articles/278901/MVVM-Pattern-Made-Simple – Deepesh May 10 '12 at 14:58
0

You can use as many data contexts as you like, as long as it is in different controls. E.g. you could have an Address UserControl which takes care of the formatting, an Addresses property on the ViewModel which holds a list of the available addresses, and a CurrentAddress property which hold the selected address out of the Addresses list. If the address has several lines, you might want to make an Address an object, not just a string.

Your XAML could look something like this:

<Page>
    <!-- Page DataContext would be set in code behind. This would be the main ViewModel -->
    <my:AddressUserControl DataContext="{Binding CurrentAddress}"/>
    <ComboBox ItemsSource="{Binding Addresses}" SelectedItem="{Binding CurrentAddress}" DisplayMemberPath="CompanyName"/>
    <!-- more controls -->
</Page>
hbarck
  • 2,934
  • 13
  • 16