【问题标题】:WPF - Control Template and control visibility based on dataWPF - 基于数据的控件模板和控件可见性
【发布时间】:2010-10-14 04:59:36
【问题描述】:

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

我需要和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>

【问题讨论】:

    标签: wpf listbox control-template


    【解决方案1】:

    您应该为此使用DataTrigger。试试这个:

    <ControlTemplate ... >
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding ImageUrl}" Value="{x:Null}">
                <Setter TargetName="txtblkImg" Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    【讨论】:

      猜你喜欢
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多