Search code examples
wpfxamlvisual-studio-2017designer

Show a control in designer when the Visiblity's Fallbackvalue is set to 'Hidden'


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.


Solution

  • 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.