1
我正在使用Control模板來顯示列表框項目。我想根據項目值設置控件的可見性。WPF - 基於數據的控件模板和控件可見性
我需要一樣How can I replace an image in a WPF grid with another control depending on the source data?
如何包括在我的代碼此選項。 (如果圖像源[imgUrl的]的值爲null我想設置的文本塊[txtblkImg]知名度崩潰。)
我的代碼:
<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 TextWrapping="WrapWithOverflow" Background="LightGreen" FontSize="10" Name="txtblkImg" HorizontalAlignment="Center" VerticalAlignment="Center" Height="74" Width="75">
<TextBlock Text="{Binding Title}"/><LineBreak/><LineBreak/>
<TextBlock Text="by "/>
<TextBlock Text="{Binding Author1}"/>
</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>