Is Selected Visibility of Element in DataTemplate

This DataTemplate is use in a ListBox Would like fieldTemplateDetail to only be visible when the ListBoxItem IsSelected

Visibility="{Binding Path=IsSelected, Converter=bvc}"   

Above is NOT valid syntax but that is what I want to do

<DataTemplate x:Key="fieldTemplate">
    <Border BorderBrush="Orange" BorderThickness="2" Margin="2" Padding="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=Name}" />
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=DisplayValue}" />
            <ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                        x:Name="fieldTemplateDetail"
                        Content="{Binding}"
                        Visibility="{Binding Path=IsSelected, Converter=bvc}"
                        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>
        </Grid>
    </Border>
</DataTemplate>

I have not looked at the answer from III yet but this works for the detail templates But obviously I don't want to repeat is for all the detail controls

<DataTemplate x:Key="fieldStringTemplate">            
    <StackPanel x:Name="fieldString" Visibility="Collapsed">
        <TextBox Text="{Binding Path=FieldValue}" />
    </StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger
            Binding="{Binding
            RelativeSource={RelativeSource
                Mode=FindAncestor,
                AncestorType={x:Type ListBoxItem}},
                Path=IsSelected}"
                Value="True">
            <Setter TargetName="fieldString" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

The answer from III is throwing an error Visibility TypeConverter IValueConverter does not support converting from string

<ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
            x:Name="fieldTemplateDetail"
            Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                            Path=IsSelected, Converter=bvc}"
            Content="{Binding}"
            ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

This seems to work

<local:FieldTemplateSelector x:Key="fieldTemplateSelector"/>
<DataTemplate x:Key="fieldTemplate">
    <Border BorderBrush="Orange" BorderThickness="2" Margin="2" Padding="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=Name}" />
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=DisplayValue}" />
            <ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                        Visibility="Collapsed"
                        x:Name="fieldTemplateDetail"
                        Content="{Binding}"
                        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>
        </Grid>
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger
            Binding="{Binding
            RelativeSource={RelativeSource
                Mode=FindAncestor,
                AncestorType={x:Type ListBoxItem}},
                Path=IsSelected}"
                Value="True">
            <Setter TargetName="fieldTemplateDetail" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Answers


Update your binding to this

<ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                  x:Name="fieldTemplateDetail"
                  Content="{Binding}"
                  Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                               Path=IsSelected, Converter={StaticResource bvc}}"
                  ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

UPDATE: I have to add the StaticResource in order for it to find the converter.


Need Your Help

Python how to find my smtp port no and host

python

I am trying to use the smtplib package in python to send mail

Robocopy: copy files preserving folder structure but adding a subfolder

windows powershell command-line directory robocopy

I need to copy a folder which contains subfolders with files to a shared folder on another machine. During that the script should create a custom sub folder in each destination folder and place files