Blue Flower

In the previous part of we met with anchor extensions and figured out how to apply them in practice, for example, to locate. Today, the library will continue to study the characteristics of Aero Framework and will consider quite an interesting topic about the data in the context of injection XAML-markup representations , and at the same time apply the knowledge from the previous article.

In practice, often occurs following problem: I twist tie model that is stored in the IoC containers running, with one or more of its views (screens). Typically, such binding occurs in behaind code, resulting in the submission set the desired value in the property DataContext .

In many cases, it works fine, but with this approach, involve certain nuances and difficulties. For example, they relate to a different contextual menus and pop-up animation, because it is not included in a visual tree, and therefore it becomes inaccessible to the main data context. Another case is related to the work list elements when the context is already an item in the list, but there is a need to use another binding source. The third option arises when one idea works with several twist models.

All these problems can be solved one way or another, but there is a universal and very simple way to solve them nicely. On it will be discussed.


To start to define the terminology. Each elementary visual inspection - is a small atomic view . Sophisticated complex representations are based on simple, forming a tree structure ( visual tree ), where each node is also a representation of the way to the root. We distinguish a special kind of ideas - screens , which in one way or another support the navigation and are often the root
.
Let them have a single repository Store , from which is derived the right on the key object instance. The idea is that by extending xaml-marking enable the extraction of any instance of the object and its further injection as context data in any visual tree node.

Everything looks very simple:

<Control DataContext="{Store Key=viewModels:AppViewModel}"/>

<Control>
	<Control.DataContext>
		<Store Key=viewModels:AppViewModel>
	</Control.DataContext>
</Control>

Get access to the twist-models from C # -code is also extremely easy:

var appViewModel = Store.Get<AppViewModel>();
var userViewModel = Store.Get<IUserViewModel>();

Moreover, WPF allows to perform even in injecting a binding!

<Slider
	DataContext="{Store Key=viewModels:MapViewModel}"
	Minimum="{Bindind MinimumZoomValue}"
	Maximum="{Binding MaximumZoomValue}"
	Value="{Binding ZoomValue, Mode=TwoWay}"
	Visibility="{Binding ShowSlider, Source="{Store Key=viewModels:SettingsViewModel}", Converter={StaticResource TrueToVisibleConverter}}"/>

Note the string Visibility = "{Binding Show Slider, Source =" {Store Key = viewModels: SettingsViewModel} " ..., this flexibility difficult to achieve even with behain code, and our record came very concise.

In fairness it must be said that the parser markup on many other platforms require prefixes, and do not support nested extensions, but this problem is solved with the help of elementary Binding Extension :

<Slider
	DataContext="{m:Store Key=viewModels:MapViewModel}"
	Minimum="{Bindind MinimumZoomValue}"
	Maximum="{Binding MaximumZoomValue}"
	Value="{Binding ZoomValue, Mode=TwoWay}"
	Visibility="{m:StoreBinding Path=ShowSlider, StoreKey=viewModels:SettingsViewModel, Converter={StaticResource TrueToVisibleConverter}}"/>

For implementation details refer to the source code library Aero Framework , everything is very transparent and understandable. key is usually the same type of twist-model or the type of interface that it implements, but nothing prohibits the use of any other.

That is to link the application screen (page or screen) need only a few lines:

<!--WP7, WP8, WPF-->
<Page
	xmlns:viewModels="clr-namespace:AeroPlayer.ViewModels"
	DataContext="{m:Store Key=viewModels:SongViewModel}">
	...
</Page>

<!--WPF-->
<Window
	xmlns:viewModels="clr-namespace:AeroPlayer.ViewModels"
	DataContext="{Store viewModels:SongViewModel}">
	...
</Window>

<!--Windows Store, WP8.1-->
<Page xmlns:viewModels="using:AeroPlayer.ViewModels">  
    <Page.DataContext>
		<Store Key=viewModels:AppViewModel>
	</Page.DataContext>
	...
</Page>

And no bihaynd code! From the contextual menu now all very elegantly:

<ContextMenu DataContext="{Store viewModels:AppViewModel}">
	...
</ContextMenu>

But how beautifully solved similar situations:

<ListBox DataContext={Store viewModels:AppViewModel} ItemsSource={Binding Persons}>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Text="{Binding LastName}"/>
                <TextBlock 
                	Text="{Binding Age}"
                	Visibility="{StoreBinding Path=ShowDetails, StoreKey=viewModels:SettingsViewModel, Converter={StaticResource TrueToVisibleConverter}}"/>
            </StackPanel>
        </DataTemplate>
    <ListBox.ItemTemplate>
</ListBox>

I hope that you have wanted to put into the consideration the campaign. This is the realization of of the principle of direct injections ( Direct Injections Principle ) , which is offered in the article . Note, in one twist model may be several representations, however, the reverse situation, when the view is working with several models twist on prakike rare due to technical difficulties described above. But with the help of direct injection ratio of View-Model-View odnin easily expands to many to many to many.

Thank you for your interest!