我在列表框中显示图像。如果图像URL没有图像,则表示将显示带有书名和作者姓名的文本块。
问题:如果图像可用,我想隐藏文本块。
代码:
<local:ImageConverter x:Key="myImageConverter"/>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Width="150">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image HorizontalAlignment="Center" Grid.Row="0" VerticalAlignment="Center" x:Name="img" Source="{Binding ImageUrl}" Height="74" Stretch="Fill" Width="75"/>
-<TextBlock Name="txtblkImg" HorizontalAlignment="Center" VerticalAlignment="Center" Height="74" Width="75">
<TextBlock Text="{Binding Title}"/><LineBreak/>
<TextBlock Text="by "/>
<TextBlock Text="{Binding Author1}"/>
</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:1)
您可以在ViewModel中公开一个描述Image是否存在的布尔值,然后使用BooleanToVisibilityConverter将三个TextBlocks(或包装它们的面板)上的可见性设置为图像上的绑定。
C#:
public bool ImageUrlIsNotValid
{
get
{
// Test to see if the specified file exists
}
}
XAML: 在资源
<BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/>
并在上面显示的代码中:
<StackPanel Orientation="Horizontal" Visibility="{Binding ImageUrlIsNotValid, Converter={StaticResource boolToVisibilityConverter}}">
<TextBlock Text="{Binding Title}"/><LineBreak/>
<TextBlock Text="by "/>
<TextBlock Text="{Binding Author1}"/>
</StackPanel>