开发者

XAML DataContext and ViewModel Type

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

Therefore the XAML controls have no idea to which instance type its DataContext is set to. T开发者_开发知识库he consequence is that there is no refactoring support and intellisense for the bound properties of my viewmodel in XAML.

Is there a way to tell a control in XAML to which type its DataContext is linked?

So when I modify a property name in my ViewModel or search for all references of that property, I want that this property in XAML bindings gets considered too.


There is no framework support, the best you can do is tell the VS designer the 'shape' of the DataContext so that it will give you hints for the properties. If you want to make your solution more refactor-proof, I would recommend Daniel's T4 metadata solution:

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

This generatesmetadata for your view models which you can reference in the XAML:

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

Colin E.


No, as the DataContext can change at runtime, it doesn't make sense to tie this to a particular type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜