locked
Collapsing controls when data binding is null RRS feed

  • Question

  • Hello,

     I have a simple pair of TextBlocks.  One has a value that is obtained through a data binding.  The other is a title label for it.  They are contained within a grid, as shown below.

    1    <Grid>
    2        <Grid.ColumnDefinitions>
    3            <ColumnDefinition Width="*" />
    4            <ColumnDefinition Width="Auto" />
    5        </Grid.ColumnDefinitions>
    6        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold" Text="First Name:" />
    7        <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" />
    8    </Grid>
    

     When the FirstName value exists, this looks nice, and has the appearance of "First Name:     Kyle".  However, if FirstName does not exist, I want to collapse the entire grid.  My goal is to not see the "First Name:" label when there is no FirstName.

    My first thought was to use a converter to convert the null value to Visibility.Collapsed.  Below shows my idea (the part where I define the nullVisibilityConverter resource is omitted):

    1    <Grid Visibility="{Binding FirstName, Converter={StaticResource nullVisibilityConverter}}">
    2        <Grid.ColumnDefinitions>
    3            <ColumnDefinition Width="*" />
    4            <ColumnDefinition Width="Auto" />
    5        </Grid.ColumnDefinitions>
    6        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold" Text="First Name:" />
    7        <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" />
    8    </Grid>
    
     
    1    public class NullVisibilityConverter : IValueConverter
    2    {
    3        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    4        {
    5            return (value != null) ? Visibility.Visible : Visibility.Collapsed;
    6        }
    7    
    8        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    9        {
    10           throw new NotSupportedException();
    11       }
    12   }
    

    However, from what I can tell in the debugger, the Convert method is only called when FirstName is not null.  Whenever FirstName is null, that binding apparently just doesn't happen.

    Is there a way I can make this converter work?  Alternatively, is there some other way of doing this? 

    Thanks,
    KPF

    Wednesday, September 2, 2009 5:47 PM

Answers

  • If No Value is assigned to the FirstName it will be null and IValueconverter will not get triggred.
    Here, What you can do is assign an empty string to the Property and condition should be checking an empty string (string.IsNull.Empty), instead of Null;

    Thursday, September 3, 2009 4:07 AM

All replies

  • If No Value is assigned to the FirstName it will be null and IValueconverter will not get triggred.
    Here, What you can do is assign an empty string to the Property and condition should be checking an empty string (string.IsNull.Empty), instead of Null;

    Thursday, September 3, 2009 4:07 AM
  • I guess that would work. It is a bit annoying, as the object I am databound to is the result of a web service that I can't modify, so I will have to have code that reassigns the null values to empty strings. Also, the empty string works fine for FirstName, but there are more complex objects that I am using in a similar fashion, so I'll have to assign a default object to those.
    Thursday, September 3, 2009 9:03 AM