WPF-基于数据的控件模板和控件可见性
我正在使用控件模板显示列表框项目。我想基于项值设置控件的可见性 我需要和你一样的 如何在我的代码中包含此选项。(如果图像源[ImgUrl]值为空,我想将textblock[txtblkImg]可见性设置为折叠。) 我的代码:WPF-基于数据的控件模板和控件可见性,wpf,listbox,control-template,Wpf,Listbox,Control Template,我正在使用控件模板显示列表框项目。我想基于项值设置控件的可见性 我需要和你一样的 如何在我的代码中包含此选项。(如果图像源[ImgUrl]值为空,我想将textblock[txtblkImg]可见性设置为折叠。) 我的代码: <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Transpar
<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>
对此,您应该使用。试试这个:
<ControlTemplate ... >
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ImageUrl}" Value="{x:Null}">
<Setter TargetName="txtblkImg" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
对此,您应该使用。试试这个:
<ControlTemplate ... >
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ImageUrl}" Value="{x:Null}">
<Setter TargetName="txtblkImg" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>