views:

114

answers:

1

Hello,

I speak about josh smith article.

can anyone show me please how the CustomerView.xaml specifically this:j

 <TextBox 
  x:Name="firstNameTxt"
  Grid.Row="2" Grid.Column="2" 
  Text="{Binding Path=FirstName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
  Validation.ErrorTemplate="{x:Null}"
  />

Why is there a Binding to FirstName which is public property in the CustomerViewModel.

There is a datacontext set for the MainViewModel, but not for the CustomerViewModel, so why does the binding work ???

A: 

Check out the ResourceDictionary in MainWindowResources.xaml. Josh uses the following code to describe what View should be used if an instance of CustomerViewModel is shown in the main window:

   <DataTemplate DataType="{x:Type vm:CustomerViewModel}">
        <vw:CustomerView />
   </DataTemplate>

We've described that the when our DataType is of Type CustomerViewModel, we'll create a new instance of the CustomerView. WPF takes care of the DataContext and creation when it sees the CustomerViewModel type.

From the rest of the article:

Applying a View to a ViewModel MainWindowViewModel indirectly adds and removes Workspace ViewModel objects to and from the main window's Tab Control. By relying on data binding, the Content property of a TabItem receives a ViewModelBase-derived object to display. ViewModelBase is not a UI element, so it has no inherent support for rendering itself. By default, in WPF a non-visual object is rendered by displaying the results of a call to its ToString method in a TextBlock. That clearly is not what you need, unless your users have a burning desire to see the type name of our ViewModel classes! You can easily tell WPF how to render a ViewModel object by using typed DataTemplates. A typed DataTemplate does not have an x:Key value assigned to it, but it does have its DataType property set to an instance of the Type class. If WPF tries to render one of your ViewModel objects, it will check to see if the resource system has a typed DataTemplate in scope whose DataType is the same as (or a base class of) the type of your ViewModel object. If it finds one, it uses that template to render the ViewModel object referenced by the tab item's Content property. The MainWindowResources.xaml file has a Resource Dictionary. That dictionary is added to the main window's resource hierarchy, which means that the resources it contains are in the window's resource scope. When a tab item's content is set to a ViewModel object, a typed DataTemplate from this dictionary supplies a view (that is, a user control) to render it, as shown in Figure 10.

The DataContext for the MainViewModel in App.xaml.cs serves as a starting point for our application.

Jimmy Lyke
Additionally, check out your output window in Visual Studio for trouble with Bindings firing.
Jimmy Lyke
But how can I use a datatemplated CustomerViewModel when the constructor is not empty? then I get a error message in xaml...
msfanboy
quote:"...If it finds one, it uses that template to render the ViewModel object referenced by the tab item's Content property..."how does the resource system handle the parameter of the ViewModel object? because the CustomerViewModel has one...
msfanboy
how stupid he wraps the vm`s in workspace and binds to them... this will lead to user interfaces where we have to open many tabs for many customers to be added.This is a terrible user experience!
msfanboy