我有一个列表,以gridview样式显示一堆图像,以下是我的代码:
<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>
PhotoConverter检查MemoryPhoto变量并返回Visibility.Visible或Collapse,具体取决于MemoryPhoto变量是否为null。以下是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();
}
}
但是当我运行我的应用程序时,我得到了这个结果,。第二个网格应该是不可见的,因为它包含空图像变量。
有人知道如何禁用wrappanel中的单个项目吗?非常感谢
修改 我想我找到了解决我的问题的方法,在图像控件中定义宽度和高度而不是在wrappanel中,代码是
<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>
答案 0 :(得分:1)
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();
}
}
<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>