1

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>
2
  • How will you be able to select the ListBoxItem if it's not visible (assuming this DataTempalte is applied as ItemTempalte of your listBox)?
    – Rohit Vats
    Commented Feb 3, 2014 at 20:02
  • It's not the entire ListBoxItem that he wants to hide, if you look closely it has TextBlock for Name and DisplayValue only the ContentPresent he wants to hide. Commented Feb 3, 2014 at 20:06

1 Answer 1

1

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.

4
  • Getting an error on Visisbility TypeConverter IValueConverter does not support converting from string
    – paparazzo
    Commented Feb 3, 2014 at 20:20
  • From system <BooleanToVisibilityConverter x:Key="bvc" /> See question. I think I got it working.
    – paparazzo
    Commented Feb 3, 2014 at 20:28
  • Oh whoops, let me update it. I forgot to add the StaticResource so you can't just use cvs on the binding that's why it is throwing. Here is the shorter code. :) Commented Feb 3, 2014 at 20:33
  • That worked. My last problem is getting Style applied when it is not selected.
    – paparazzo
    Commented Feb 3, 2014 at 20:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.