fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: X# and DataContext in XAML

X# and DataContext in XAML 2 years 1 week ago #1945

  boonnam's Avatar Topic Author boonnam Offline Posts: 36
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 "
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:

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

X# and DataContext in XAML 2 years 1 week ago #1946

  robert's Avatar robert Offline Posts: 969
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
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.

X# and DataContext in XAML 2 years 1 week ago #1947

  NickFriend's Avatar NickFriend Offline Posts: 184
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.
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

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

X# and DataContext in XAML 2 years 1 week ago #1949

  Phil Hepburn's Avatar Phil Hepburn Offline Posts: 742
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' :-





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

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

  • Page:
  • 1