How do I bind data to a ViewModel in Expression Blend?

In WPF and Silverlight, you can create a view model object and set it to the DataContext of a control when the control is constructed at run time. Then you can bind the properties of the visual to properties in your DataContext.

One way to set an anchor is to inject the anchor directly into the tag:

<TextBox Text="{Binding Path=Description}"/>

      

And that will bind the textbox to the Description property in the view model.

The problem with typing into a binding is that you can make an input error. (And you will almost certainly make a mistake if you have hundreds of bindings to make.)

In Expression Blend, there is a small white dot next to the Text property in the Properties window. This brings up a menu from which you can create a data binding.

  • How can I get the properties of the view model to display in the Create Data Binding dialog so I can select them.
  • Will the data binding configuration in Blend interfere with setting my ViewModel to the DataContext at runtime?
+2


source to share


3 answers


One way is to include the virtual machine as a resource in your view:

<UserControl>
    <UserControl.Resources>
        <local:YourViewModel x:Key="ViewModel"/>
    </UserControl.Resources>
</UserControl>

      



Then you can refer to this as DataContext="{StaticResource ViewModel}"

elsewhere.

I can't say that I like it, but I cannot say that I, like any of the species-first idiosynchrasies, are that mixture imposed on your design.

+1


source


I experimented with Blend to find a drag and drop data binding approach that still makes it easy to override your view model in code.

  • First create your viewmodel object that implements INotifyPropertyChanged and raises a notification event in setters. View models can be hierarchical. For example, you might have an ObservableCollection in your main view model.
  • In Blend, open a page or control and go to the data tab.
  • On the right, open the menu under the Add Live Data Source icon.
  • Select "Define Data Source of New Object"
  • Select the top-level model class and confirm the dialog

In my experiments, I found it important to link the datasource to where I wanted it, or Blend might make a less optimal configuration if I didn't take the next step first.

  • Open the Objects and Timeline window in Blend
  • Select the root object like UserControl
  • Open Properties and make sure the root is selected.
  • Find DataContext and click the square to open the menu and select DataBinding
  • Select the data source that was just created.

Now that the data source has been created, data binding is very easy.



  • set some controls on the page
  • open data window
  • from the DataSource for your display model, drag model to controls to create bindings or set binding in the properties window.

You can now create a live view model object in the control constructor

public MainPage()
    {
        // Required to initialize variables
        InitializeComponent();
        mVm = new MyViewModel();
        this.DataContext = mVm;
    }
    private MyViewModel mVm;

      

Add any initialization to fetch data and you are ready to go.

+1


source


I have a screen shot on my blog Blendable MVVM: Introduction and Data Binding that shows setting this up for Siverlight.

Essentially, you create the ViewModel as a DataContext UserControl using the New Object Initializer, and then use the Explicit Data Context tab of the Binding dialog box for the controls themselves.

Hope it helps.

+1


source







All Articles