X# and DataContext in XAML

This forum is meant for questions and discussions about the X# language and tools
Post Reply
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

X# and DataContext in XAML

Post by boonnam »

Here is a snippet of my XAML (see attachment for the error):

<UserControl x:Class="xWpfAppDemo.GroupExplorer"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:xWpfAppDemo"
xmlns:vm="clr-namespace:xWpfAppDemo.ViewModel"
DataContext="{DynamicResource ViewModelGroupExplorer}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
<UserControl.Resources>
<vm:ViewModelGroupExplorer x:Key="ViewModelGroupExplorer"/>
</UserControl.Resources>

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.

Thanks,
Boonnam
Attachments
XAML error 2.JPG
XAML error 2.JPG (17.36 KiB) Viewed 401 times
XAML error 1.JPG
XAML error 1.JPG (54.76 KiB) Viewed 401 times
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

X# and DataContext in XAML

Post by robert »

Boonnam,

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.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

X# and DataContext in XAML

Post by NickFriend »

Hi,

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.

Code: Select all

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.

HTH

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

X# and DataContext in XAML

Post by Phil Hepburn »

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)

Cheers,
Phil.
Wales, UK.

Here are a couple of image 'pointers' :-
DataContInCode_01.jpg
DataContInCode_01.jpg (109.25 KiB) Viewed 401 times
DataContInCode_02.jpg
DataContInCode_02.jpg (90.56 KiB) Viewed 401 times
The above is the best I can do at the moment, off the top of my head !
Post Reply