1

This code work well, but the button visibility is collapsed in the design.

how can i set this to visible?

<!--Resources-->
<BooleanToVisibilityConverter x:Key="BoolToVis" />


<Button Visibility="{Binding Converter={StaticResource BoolToVis}, Source={x:Static local:ConfigUser.Prc}}"  Grid.Row="1"/>

2 Answers2

2

If I get right what you want. What you need is the button to appear in design mode and also appear when your boolean is set to true at runtime.

You can create your converter that test if it's in design mode in addition of your boolean:

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
public class DesignVisibilityConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value is bool) {
            return ((bool) value) || DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow)
                ? Visibility.Visible
                : Visibility.Collapsed;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}
nkoniishvt
  • 2,442
  • 1
  • 14
  • 29
  • 1
    `DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow)` can fail when you are not using a main window. Replace that with `DesignerProperties.GetIsInDesignMode(new DependencyObject())` and it works like a charm. Nonetheless, I like your implementation a lot. – Christian Ivicevic Jan 29 '17 at 00:15
  • @ChristianIvicevic Good suggestion. Thank you! – nkoniishvt Feb 06 '17 at 13:23
1

This one defaults to visible. Here is my Inverse BoolToVis

[ValueConversion(typeof(bool), typeof(Visibility))]
public class InverseBooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value != null && (bool)value) ? 
            Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value != null) && ((Visibility)value == Visibility.Collapsed);
    }
}
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122