Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-基于数据的控件模板和控件可见性_Wpf_Listbox_Control Template - Fatal编程技术网

WPF-基于数据的控件模板和控件可见性

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

我正在使用控件模板显示列表框项目。我想基于项值设置控件的可见性

我需要和你一样的

如何在我的代码中包含此选项。(如果图像源[ImgUrl]值为空,我想将textblock[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>

对此,您应该使用。试试这个:

<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>