1

I have a list that displays a bunch of images in gridview style, the following is my code:

<controls:PanoramaItem x:Name="Pano_Photos" Header="photos">
        <ListBox x:Name="lstMemoriesPhoto" ItemsSource="{Binding MemoryList}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged" x:Name="ListPhotoSelectionChangedEventTrigger">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding NavigateToDetailPage}" PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel ItemWidth="130" ItemHeight="130" Visibility="{Binding MemoryPhoto,Converter={StaticResource PhotoConverter}}"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding MemoryPhoto,Converter={StaticResource ByteImageConverter}}" Margin="5"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>        
    </controls:PanoramaItem>

The PhotoConverter checks MemoryPhoto variable and returns Visibility.Visible or Collapse depending on whether the MemoryPhoto variable is null or not. Here is the code for PhotoConverter:

  public class PhotoConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if ((value is byte[]) && (value != null))
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

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

But When I run my app, I've got this result,second grid should be invisible. The second grid should be invisible because it contains null image variable.

Does anyone know how to disable individual item in wrappanel? Thanks a lot

Edit I think I found the solution for my issue, define width and height in image control rather than in wrappanel, the code is

<ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding MemoryPhoto,Converter={StaticResource ByteImageConverter}}" Margin="5" Visibility="{Binding MemoryPhoto,Converter={StaticResource PhotoConverter}}" width="130" height="130"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
1
  • if I change <toolkit:WrapPanel ItemWidth="130" ItemHeight="130" Visibility="{Binding MemoryPhoto,Converter={StaticResource PhotoConverter}}"/> to <stackpanel orientation="horizontal" Visibility="{Binding MemoryPhoto,Converter={StaticResource PhotoConverter}}"/>. Everything is fine.
    – Alex
    Commented Feb 8, 2012 at 0:30

1 Answer 1

1

First please fix your converter:

public class PhotoConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if ((value is byte[]) && (value != null))
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        catch (Exception ex)
        {
            throw ex;
            return Visibility.Collapsed;
        }
    }

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

Then fix your XAML

<controls:PanoramaItem x:Name="Pano_Photos" Header="photos">
    <ListBox x:Name="lstMemoriesPhoto" ItemsSource="{Binding MemoryList}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged" x:Name="ListPhotoSelectionChangedEventTrigger">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding NavigateToDetailPage}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel ItemWidth="130" ItemHeight="130"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding MemoryPhoto,Converter={StaticResource ByteImageConverter}}" Visibility="{Binding MemoryPhoto, Converter={StaticResource PhotoConverter}}" Margin="5"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>        
</controls:PanoramaItem>
2
  • Your code produces a same result as the image shows in my question post. The second grids shows nothing, but what I want is that the grid with no image on it should be invisible.
    – Alex
    Commented Feb 8, 2012 at 0:52
  • It seems that the WrapPanel ignores the Visibility property for the contained item, whenever ItemWidth is set.
    – BladeWise
    Commented Feb 12, 2014 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.