Silverlight 4: how to switch control visibility

后端 未结 2 1946
清酒与你
清酒与你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 22:46

    I've faced the problem of binding a Boolean value to the visibility property, so I've implemented my own Boolean to Visibility Converter, I'm using it with most of my applications.

    Add the Following Class to your application:

    public class BoolVisibilityConverter : IValueConverter{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
            bool isVisible = (bool)value;
            return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){
            System.Windows.Visibility currVisibility = (System.Windows.Visibility)value;
            return (currVisibility == System.Windows.Visibility.Visible);
        }
    }
    

    Now To Use it you'll need to add it as a resource in your XAML Code.

    
        
    
    

    In your example use the following:

    
    
    
    

提交回复
热议问题