WPF: Binding Visibility by string contents

后端 未结 5 1085
孤城傲影
孤城傲影 2021-01-07 22:55

Ok, so here is my XAML:


If InstanceName is null or an

5条回答
  •  再見小時候
    2021-01-07 23:47

    You could use a ValueConverter:

    
    

    with the following codebehind:

    public class StringNullOrEmptyToVisibilityConverter : System.Windows.Markup.MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.IsNullOrEmpty(value as string) 
                ? Visibility.Collapsed : Visibility.Visible;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {            
            return this;
        }
    }
    

提交回复
热议问题