0

I have custom control in a separate project. In my main project visibility properties of some controls are connected to this control. For example:

<Canvas x:Name="groupControls" Visibility="{Binding IsActive, ElementName=MyControl, Converter={StaticResource BooleanToVisibilityConverter}}">

During design time the property returns false and groupControls becomes invisible so I cannot see it. I want to prevent from designer to evaluate the property as if it was defined in the same project.

Using DesignerProperties.IsInDesignModeProperty does not solve the problem because some controls are visible when the value is true and some when it is false.

sergman
  • 171
  • 1
  • 2
  • 12

2 Answers2

0

I don't know how to do this and I believe it was not designed to work that way.

You can change you Converter to implement IMultiValueConverter and use the Window property IsLoaded as a second parameter to do that.

Create a DependencyProperty, WindowIsLoaded for example, then, on Loaded event change the value to true.

On Converter, when WindowIsLoaded is equals to false always return Visibility.Visible.

ON CODE:

public bool WindowIsLoaded 
{
    get { return (bool)GetValue(WindowIsLoadedProperty); }
    set { SetValue(WindowIsLoadedProperty, value); }
}
public static readonly DependencyProperty WindowIsLoadedProperty =
        DependencyProperty.Register("WindowIsLoaded", typeof(bool), typeof(Window),
            new PropertyMetadata(false));

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    WindowIsLoaded = true;
}

public class BooleanToVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var isActivated = (bool)values[0];
        var isLoaded = (bool)values[1];

        if (!isLoaded)
            return Visibility.Visible;

        return isActivated ? Visibility.Visible : Visibility.Collapsed
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("Only oneway binding!");
    }
}

XAML:

xmlns:local="clr-namespace:YourProjectNamespace.YourWindow"
<Canvas x:Name="groupControls">
    <Canvas.Visibility>
        <MultiBinding Converter="{StaticResource BooleanToVisibilityConverter}">
            <Binding ElementName="MyControl" Path="IsActivated"/>
            <Binding RelativeSource="{RelativeSource AncestorType={x:Type local:MainWindow},
                                      Mode=FindAncestor}" 
                     Path="WindowIsLoaded" />
        </MultiBinding>
    </Canvas.Visibility>
</Canvas>

Now the Designer will recive false from WindowIsLoaded and all your controls will be visible on Designer Mode.

Henryk Budzinski
  • 1,161
  • 8
  • 20
0
<Canvas x:Name="groupControls" Visibility="{Binding IsActive,
                                                    FallbackValue=True,                
                                                    ElementName=MyControl, 
                                                    Converter={StaticResource BooleanToVisibilityConverter}}">

or the designmode is what you need

Is there a way to check if WPF is currently executing in design mode or not?

Community
  • 1
  • 1