Reputation: 12880
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.
Upvotes: 8
Views: 5928
Reputation:
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>
Upvotes: 5
Reputation: 45096
Not sure it is a lot cleaner but you should set it to Visible in the ctor (before the Initialize);
Upvotes: 2
Reputation: 189
Have you seen Hide WPF elements in Visual Studio designer? It looks like other people solved the issue by creating a simple custom extension.
Upvotes: 1