WPF / XAML - hide image if other control text property is empty / not set

I am new to wpf and this is my first attempt at creating a custom control. its purpose is to display two values ​​(myText1 and myText2) with their respective images (myimage1, myimage2). sometimes one of these values ​​is not specified and therefore one image must also be hidden. here's my code:

Window1.xaml

<local:myControl myText2="Hello World!" />

      

myControl.xaml

<TextBlock Text="{Binding ElementName=myControl,Path=myText1}" />
<Image Source="myimage1.jpg" />

<TextBlock Text="{Binding ElementName=myControl,Path=myText2}" />
<Image Source="myimage2.jpg" />

      

myText1 was not set to window1.xaml, so the text block remains empty. but the image is still displayed. what lines of code am I missing to hide the image if myText1 (or myText2) was not set in window1.xaml?

+2


source to share


3 answers


You have a post converter for text visibility

public class TextToVisibilityConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string && targetType == typeof(bool))
        {
            if (value.ToString().Equals(string.Empty))
                 return Visibility.Hidden;
            else
                return Visibility.Hidden;
         }
        else
        {
            return null;
        }
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Visibility && targetType == typeof(string))
        {
            if ((Visibility)value == Visibility.Visible)
            {
                return "Text";
            }
            else
            {
                return string.Empty;
            }
        }
        else
        {
            return null;
        }
    }
}

      



And in XAML <TextToVisibilityConverter x: Key = "myCnverter" / ">

+2


source


A couple of small mistakes in there:

if (value is string && targetType == typeof(bool))  
    {            
    if (value.ToString().Equals(string.Empty))  
        return Visibility.Hidden;  
    else  
        return Visibility.Hidden;  
    }

      

Should be

if (value is string && targetType == typeof(Visibility))  
    {            
    if (value.ToString().Equals(string.Empty))  
        return Visibility.Hidden;  
    else  
        return Visibility.Visible;  
    }

      



You will need the following messages:

using System.Windows;
using System.Windows.Data;

      

You may also consider returning Visibility.Collapsed

rather thanVisibility.Hidden

+1


source


Once you create the right converter, it's easy for you. And not many answers also got that Text.IsEmpty is available for the TextBlock Text property

I created a BooleanVisibilityConverter which depends on the boolean parameter being True or False. Gives you true flexibility not found in xaml.

public class BooleanVisibilityConverter : IValueConverter
{       
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility v = Visibility.Collapsed;
        bool checkValue = true;
        if(parameter != null)
        {
            checkValue = Boolean.Parse(parameter.ToString());
        }
        if(value.Equals(checkValue))
        {
            v = Visibility.Visible;
        }
        return v;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

      

Then in your xaml, import that namespace into:

xmlns:conv="clr-namespace:ConvertersNamespace"

      

Create a converter in Resources:

<UserControl.Resources>
    <conv:BooleanVisibilityConverter x:Key="bool2vis" />        
</UserControl.Resources>

      

Then just use in your design:

<TextBlock Text="{Binding ElementName=myControl,Path=myText1}" x:Name="txtBlock"/>
<Image Source="myimage1.jpg"  
       Visibility="{Binding ElementName=txtBlock,Path=Text.IsEmpty,
                     Converter={StaticResource bool2vis},ConverterParameter=False}"/>

      

0


source







All Articles