8

I have a control that has Hidden visibility because it is bound to a property in the View Model whose default value causes it to be hidden. I can access it through the XAML but I'd like it still shown in the designer.

Is there a clean way of doing this? For now, I'm manually editing the Visibility attribute to make it show up, but I'd rather not have to do that, in case I forget to change it back.

jglouie
  • 12,523
  • 6
  • 48
  • 65

3 Answers3

5

You can bind to the boolean attached property DesignerProperties.IsInDesignMode, which is true only if you are inside the designer. Here is an example:

<Window x:Class="Visitest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cm="clr-namespace:System.ComponentModel;assembly=PresentationFramework" 
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="conv"/>
    </Window.Resources>
    <Grid>
        <TextBox Margin="8" Background="Green" 
                 Visibility="{Binding (cm:DesignerProperties.IsInDesignMode), RelativeSource={RelativeSource Self}, Converter={StaticResource conv}}"/>           
    </Grid>
</Window>
  • 1
    I clarified my question a bit. The Visibility is already bound to an item in the View Model, whose default value causes it to be hidden. Maybe I could change the binding to what you have here in the constructor if I'm in Design Mode? – jglouie Mar 26 '12 at 18:19
2

Not sure it is a lot cleaner but you should set it to Visible in the ctor (before the Initialize);

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Care to explain down vote on a accepted answer two years old? – paparazzo Mar 14 '14 at 18:42
  • It's not me who down voted. But I can't understand how your answer could possibly help. I don't think the designer reads the code behind file at all? – Arek Feb 09 '16 at 08:59
  • @Arek Exactly designer does not read the code behind and it has the check mark. – paparazzo Feb 09 '16 at 09:14
  • okay, then how setting anything in the constructor would make hidden control visible in the designer? – Arek Feb 10 '16 at 10:46
1

Have you seen Hide WPF elements in Visual Studio designer? It looks like other people solved the issue by creating a simple custom extension.

Community
  • 1
  • 1
Yossarian21
  • 149
  • 1
  • 4