tags:

views:

1253

answers:

2

hello everybody,

i'm new to wpf and this is my first attempt of creating a custom user control. its purpose is to display two values (myText1 and myText2) with their corresponding images (myimage1, myimage2). sometimes, one of these values is not set and therefore oneimage should be hidden as well. here's my code so far:

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 in window1.xaml and therefore the textblock remains empty. but the image is still displayed. which lines of code am i missing to hide the image if myText1 (or myText2) was not set in window1.xaml?

+1  A: 
Firoz
A: 

Couple of small mistakes 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 need the following usings:

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

You may also consider returning Visibility.Collapsed rather than Visibility.Hidden

PaulB