Artem Shaban
Artem Shaban

Reputation: 176

Not working Count to Visibility

I need show grid when Count > 0, else collapse it.

<Grid HorizontalAlignment="Left"
                    Visibility="{Binding Num.Count, Converter={StaticResource IntToVisibleConverter}}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="Image.jpg"
                        Height="{Binding ActualHeight, ElementName=TextBlock, UpdateSourceTrigger=PropertyChanged}"
                        Grid.Column="0"/>
                    <TextBlock x:Name="TextBlock"
                        Grid.Column="1"
                        Text="{Binding Num, Converter={StaticResource NumStrConverter}}"/>
                </Grid>

I use this converter

class IntToVisibleConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        int val = (int) value;
        if (val == 0)
            return Visibility.Collapsed;
        return Visibility.Visible;
    }
...
}

public IEnumerable<string> Num{ get; set;}

image show image.jpg when Count==0

Upvotes: 0

Views: 573

Answers (1)

Chris
Chris

Reputation: 5514

Since your Num is IEnumerable<string>, when you write Num.Count you are actually referring to the Count extension method. Binding in WPF works with properties, so that won't work. If you look at the debug output, you should get a binding error.

So, you need to make sure you bind to a property, not a method. Like Default mentions, switch to ObservableCollection<T> or something similar, that will both give you a Count property, and update notifications whenever the collection changes.

Upvotes: 1

Related Questions