Reputation: 14930
I have a listbox which has templates defined for the ItemContainer and the ItemTemplate styles.
My ItemTemplate is a DataTemplate which is very simple:
<DataTemplate x:Key="DataTemplate1">
<Grid x:Name="grid">
<TextBlock TextWrapping="Wrap" Text="{Binding}" Foreground="White" FontSize="24" />
</Grid>
</DataTemplate>
and the ItemContainer is also very simple:
<Grid x:Name="Grid" HorizontalAlignment="Stretch" Height="Auto" SnapsToDevicePixels="true" Width="373" Background="{x:Null}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="37"/>
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected"/>
<VisualState x:Name="SelectedUnfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="contentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="2,0,0,0" d:LayoutOverrides="Width">
</ContentPresenter>
<Image x:Name="image" HorizontalAlignment="Center" Margin="0,0,0,1" Source="/MyApp;component/Images/icon_arrowcircle.png" Stretch="Uniform" Width="37" Grid.Column="1" VerticalAlignment="Center" Opacity="0"/>
</Grid>
Which all works fine.
But what I want to be able to do is, when an item is selected (i.e. the Selected visual state) I want the TextBlock in the DataTemplate to change its Foreground color to Black instead of white and make the font size larger. But I cannot seem to find out a clean way of doing this as TemplateBinding does not seem to be available from a DataTemplate.
Any ideas?
Upvotes: 1
Views: 2521
Reputation: 41
Don't think the answer works anymore in current Silverlight versions, attached orpoerties on the ContentPresenter don't even build anymore. Anyone else figure this out?
Upvotes: 0
Reputation: 24453
If you want the template for the ItemContainer to control the Foreground you can set it there instead of inside the ItemTemplate. TextBlock.Foreground and TextBlock.FontSize can be used as attached properties and will inherit from a parent value:
<ContentPresenter TextBlock.Foreground="White" TextBlock.FontSize="18"/>
The settings for these properties will need to be removed from the ItemTemplate to avoid overriding the inherited values. You can then set whatever values you need from the VisualState animations.
Upvotes: 2