views:

427

answers:

3

I have the following stack panel

<StackPanel>
       <TextBlock Text="{Binding AddressLine1}" />
       <TextBlock Text="{Binding AddressLine2}" />
       <TextBlock Text="{Binding AddressLine3}"  />
       <TextBlock Text="{Binding AddressLine4}"  />
</StackPanel>

and my AddressLine2 string is null on the bound object.

My stack panel renders like

| AddressLine1 |
|              |
| AddressLine3 |
| AddressLine4 |

but I want it to render like

| AddressLine1 |
| AddressLine3 |
| AddressLine4 |

is this possible, or am I missing an obvious way to do it?

Thanks, Chris

A: 

You can bind the Visibility of the TextBlock to the Text property by using a ValueConverter

ArielBH
I thought that might be the case - it seems so messy - I was really hoping a little bit of XAML would do it
Chris
Can't think of a cleaner way to achieve what you need.
ArielBH
A: 

you can use Trigger for TextBlock for checking if Text is null and set the Visibility to Collapsed in that case.

viky
+2  A: 

Create a converter that implements IMultiValueConverter then use a MultiBinding on the text so that you only have one TextBlock with each line like this:

class MultiStringConverter : IMultiValueConverter
{
    public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        var text = new StringBuilder( );
        for ( int i = 0 ; i < values.Length ; i++ ) {
            string line = String.Format( "{0}", values[i] );
            if ( !String.IsNullOrEmpty( line ) ) {
                text.AppendLine( line );
            }   // if
        }
        return text.ToString( );
    }

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

And in the XAML...

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding>
                <MultiBinding.Converter>
                    <Local:MultiStringConverter />
                </MultiBinding.Converter>
                <Binding Path="AddressLine1" />
                <Binding Path="AddressLine2" />
                <Binding Path="AddressLine3" />
                <Binding Path="AddressLine4" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
Josh Einstein