X# and DataContext in XAML

1 year 7 months ago #1 by Boonnam S
X# and DataContext in XAML was created by Boonnam S
Here is a snippet of my XAML (see attachment for the error):

<UserControl x:Class="xWpfAppDemo.GroupExplorer"
xmlns=" schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" schemas.microsoft.com/winfx/2006/xaml "
xmlns:mc=" schemas.openxmlformats.org/markup-compatibility/2006 "
xmlns:d=" schemas.microsoft.com/expression/blend/2008 "
DataContext="{DynamicResource ViewModelGroupExplorer}"
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
<vm:ViewModelGroupExplorer x:Key="ViewModelGroupExplorer"/>

While learning about WPF, I came across an article explaining how to set the DataContext in XAML. I was able to do it in C# and X# without making any change to the XAML. The problem I ran into with X# is that it think "vm:ViewModeGroupExplorer" does not exist in namespace "clr-namespace:xWpfAppDemo.ViewModel", but it compile and run no problem at all. But I during development, I don't see what the UI look like, but instead I see "Invalid Markup". So with X# I simply use the code-behind to set the DataContext. This issue is still happening with beta 12. Anyone know how to fix this?

Also, I'm a lazy programmer. Will the General Release improve the intellsense so X# know the properties and methods of my classes? So it will auto complete my typing.


Please Log in or Create an account to join the conversation.

1 year 7 months ago #2 by Robert van der Hulst
Replied by Robert van der Hulst on topic X# and DataContext in XAML

The General Release will most certainly improve intellisense in the Sourcecode Editor.
However I am not sure about intellisense in the XAML editor. That may take a little longer.
Unfortunately every subsystem in VS has its own technique of adding things.
And intellisense for both editors is handled differently.


XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

1 year 7 months ago #3 by Nick Friend
Replied by Nick Friend on topic X# and DataContext in XAML

It's a long time since I was looking at this sort of detail, but the issue with not having the UI visible at design time is simply because your ViewModel is not instantiated when you're in the xaml editor.

You can get around this by the following, but the VM must have a parameterless constructor for this to work.
d:DataContext="{d:DesignInstance Type=vm:ViewModelGroupExplorer, IsDesignTimeCreatable=True}"

This works fine for design time, but assigning the runtime DataContext in xaml is probably not a good idea in the long run.



Please Log in or Create an account to join the conversation.

1 year 7 months ago #4 by Phil Hepburn
Replied by Phil Hepburn on topic X# and DataContext in XAML
Hi guys,

Since I learned my WPF stuff following Nick's samples some years ago now, it is not surprising that I agree with him on trying NOT to include the runtime DataContext in XAML.

Since getting going with Nick's User Group session samples, and developing a good number of my own, things seem to work well on the MVVM pattern by assigning the DataContext property of the View to the ViewModel in code. It is possible to still create complex Views with data templates of deeply structured data from Models.

CARE with XAML, it is so flexible and powerful that we as designer / coders can end up trying to apply more than one 'pattern' of approach at the same time. That means mixing stuff up.

Try to look at some of my published samples to see what Nick and I are referring to. My approach now seems quite simple or straight forward - thanks to Nick ;-0)

Wales, UK.

Here are a couple of image 'pointers' :-

The above is the best I can do at the moment, off the top of my head !

Please Log in or Create an account to join the conversation.