Silverlight 4: how to switch control visibility

后端 未结 2 1942
清酒与你
清酒与你 2020-12-16 22:20

I am using MVVM in my Silverlight app. When control visibility is need to be managed by data, I am connecting its \'Visibility\' property to object\'s corresponding property

2条回答
  •  暖寄归人
    2020-12-16 22:45

    I just used Reflector to inspect the type converters in the PresentationFramework.dll

    There is already an implementation that can convert between boolean and visibility. You should be able to make use of this in your silverlight application.

    public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool flag = false;
            if (value is bool)
            {
                flag = (bool) value;
            }
            else if (value is bool?)
            {
                bool? nullable = (bool?) value;
                flag = nullable.HasValue ? nullable.Value : false;
            }
            return (flag ? Visibility.Visible : Visibility.Collapsed);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((value is Visibility) && (((Visibility) value) == Visibility.Visible));
        }
    }
    

提交回复
热议问题