XAML DataContext and ViewModel Type

I use MVVM for my application, the DataContext of the controls is assigned in my C # code (and not in XAML).

Therefore, XAML controls have no idea to which instance its DataContext type is set. The consequence of this is the lack of support for refactoring and intellisense for the related properties of my view model in XAML.

Is there a way to tell the control in XAML what type its DataContext bound to?

Therefore, when I change the name of a property in my ViewModel or search for all references to this property, I want this property to be counted in XAML bindings too.

+4
source share
2 answers

There is no framework support, the best thing you can do is show the constructor VS the "form" of the DataContext so that it gives you tips for the properties. If you want to make your decision more refactored, I would recommend Daniel T4 metadata solution:

http://www.codeproject.com/KB/codegen/T4Metadata.aspx

This generates metadata for your view models, which you can reference in XAML:

 <StackPanel DataContext="{Binding Source={StaticResource Person}}"> <TextBlock >Name:</TextBlock> <TextBox Text="{Binding Path={x:Static Metadata:PersonMetadata.NamePath}}"/> </StackPanel> 

Colin E.

+3
source

No, since the DataContext can change at run time, it makes no sense to bind this to a specific type.

+2
source

Source: https://habr.com/ru/post/1332359/


All Articles