Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get strongly typed bindings in WPF/XAML?

Tags:

mvvm

wpf

Using the MVVM-pattern you set the DataContext to a specific ViewModel. Now is there any way to tell the XAML the type of the DataContext so that it will validate my bindings?

Looking for something like the typed viewdata in ASP.NET MVC.

like image 788
loraderon Avatar asked Mar 11 '09 07:03

loraderon


People also ask

How many types of binding are there in WPF?

WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.

What is data binding in WPF and XAML?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is XAML binding?

Advertisements. Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.


1 Answers

You can write each individual binding in a strongly-typed way:

<TextBox Text="{Binding Path=(vm:Site.Contact).(vm:Contact.Name)}" />

However this would not validate the fact that TextBox DataContext is of type ViewModel.Site (and I think this is not possible, but I may be wrong).

like image 90
Andrey Shchekin Avatar answered Oct 28 '22 03:10

Andrey Shchekin