chuang4630
asked on
How to add DataContext to User Control's XAML?
How to add DataContext to User Control's XAML?
I have a user control MyControl, it needs to be bound to its view model: MyControlViewModel. I have no problem to add the DataContext programmically in the constructor. But I was told this is not allowed. It has to be done in the XAML. However, I have gotten the error in XAML
MyControlViewModel is in the project MyProject.ViewModel
<UserControl .....
xmlns:local="clr-namespace :MyProject .ViewModel "
......
>
<UserControl.DataContext>
<local:MyControlViewModel/ > // ERROR: The name MyControlViewModel does not exist in the namespace "clr-namespace:MyProject.V iewModel"
</UserControl.DataContext>
<UserControl.Resources>
// Style stuff...
</UserControl.Resources>
</UserControl>
Anyone can help me?
Thanks a lot.
I have a user control MyControl, it needs to be bound to its view model: MyControlViewModel. I have no problem to add the DataContext programmically in the constructor. But I was told this is not allowed. It has to be done in the XAML. However, I have gotten the error in XAML
MyControlViewModel is in the project MyProject.ViewModel
<UserControl .....
xmlns:local="clr-namespace
......
>
<UserControl.DataContext>
<local:MyControlViewModel/
</UserControl.DataContext>
<UserControl.Resources>
// Style stuff...
</UserControl.Resources>
</UserControl>
Anyone can help me?
Thanks a lot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have a right solution
<UserControl .....
xmlns:local="clr-namespace