1

in several Views we have container components - for example Grid or ScrollViewer- that are bound to a property on their own ViewModel or ViewModelItem.

In some cases the View (parent) containing these components can be shown without the ViewModel/ViewModelItem of the components being initialized. To avoid showing a user empty datagrids we are using FallBackValue=Hidden to make sure the container is only shown when it's ViewModel/ViewModelItem is initialized/loaded.

A hypothetical example would be a Window containing a DataGrid and a more detailed view to the right. The detailed view would be its own UserControl with its own ViewModel and the DataGrid would have its own ViewModel as well. When opening said Window the ViewModel of the DataGrid would be loaded immediately - but since no row has been selected the ViewModel that belongs to the detailed View would not be intialized, which means that the Visibility Binding of the detail View would fail and the binding's FallbackValue would be used to hide the detail View. The issue with this approach is that the Visual Studio WPF/XAML designer will not show the content of the affected containers since they are hidden due to their FallbackValue.

Question: Is there a way to get the designer to show specific controls/components that have the FallBackValue of a Visibility binding set to Hidden? Clicking into the XAML code that is inside these hidden containers does not show them.


Edit 1

I found this answer. Setting d:IsHidden="false" does not help. Regardless of if it's set before or after the Visibility property.

Community
  • 1
  • 1
Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
  • Have you set the Design Data Context? We could do with some code examples. `xmlns:vm="clr-namespace:VM.ViewModel"`And then `d:DataContext ="{d:DesignInstance {x:Type vm:YourViewModelNameHere}, IsDesignTimeCreatable=True}"` – XAMlMAX Jan 05 '18 at 11:08
  • @XAMlMAX wow that's a nice snippet and it works for a few cases! The only issue I have is that in most cases the constructor of the ViewModel would call methods of other classes that - due to MEF - are unknown to the designer. – Steffen Winkler Jan 05 '18 at 11:28
  • I think Blend gives you more flexibility for creating Design Time instance have a look at [this MSDN post](https://msdn.microsoft.com/en-gb/magazine/dn169081.aspx). – XAMlMAX Jan 05 '18 at 11:57
  • @XAMlMAX yeah stumbled over this ~30 minutes ago ;). Seems a bit overwhelming at the moment. My current idea is to create a subclass of the specific types and use the default constructor of the subtype. Oh also could you re-phrase your comments into an answer? Because that essentially solved my problem. – Steffen Winkler Jan 05 '18 at 12:18

1 Answers1

1

When it comes to the designer you would have to set the Design Data Context.
To do that you would typically do the following in your xaml file:

<Control, Page or Window
    xmlns:vm="clr-namespace:VM.ViewModel"
    d:DataContext="{d:DesignInstance {x:Type vm:YourViewModelNameHere}, IsDesignTimeCreatable=True}" 
    .../>  

However, sometimes like in your case, more flexibility is needed for the complex ViewModel. For this I would use Blend and it's functionality, what that is it will generate dummy data for you and set the data context of the Control for you.
For more information refer to this MSDN article.

XAMlMAX
  • 2,268
  • 1
  • 14
  • 23
  • 1
    thank you! As for the cases where the default constructor of the ViewModel calls upon MEF or other methods that would throw exceptions I created a [subclass](https://stackoverflow.com/a/4245831/937093), added a default constructor that sets the bound properties to appropiate values. I then added a new constructor to the ViewModel that has a dummy parameter - I created a `DummyDesigner` class just for that constructor - and used the `: base(new DummyDesigner())` syntax to make the subclass's default constructor call that new constructor on the ViewModel – Steffen Winkler Jan 05 '18 at 13:02