views:

48

answers:

1

Hi All,

Hi I am using Control template to display listbox items. I want to set the visibility of the control based on the item value.

I need the same http://efreedom.com/Question/1-424562/Can-Replace-Image-WPF-Grid-Another-Control-Depending-Source-Data

How to include this option in my code. (If the image source [ImgUrl] value is null i want to set the textblock [txtblkImg] visibility to collapse.)

My Code:

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

You should use a DataTrigger for this. Try this:

<ControlTemplate ... >

    <!-- ... -->

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