2010-11-22 147 views
1

我有一个列表框,它为ItemContainer和ItemTemplate样式定义了模板。Silverlight中的TemplateBinding和ContentPresenter/WPF

我ItemTemplate中是一个DataTemplate,这是非常简单的:

<DataTemplate x:Key="DataTemplate1"> 
    <Grid x:Name="grid"> 
    <TextBlock TextWrapping="Wrap" Text="{Binding}" Foreground="White" FontSize="24" />  
    </Grid> 
</DataTemplate> 

和ItemContainer也很简单:

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

这一切工作正常。

但我希望能够做到的是,当一个项目被选中(即选定的视觉状态),我希望DataTemplate中的TextBlock将其前景颜色更改为黑色而不是白色,并使字体尺寸更大。但我似乎无法找到一个干净的方式来做到这一点,因为TemplateBinding似乎无法从DataTemplate中获得。

任何想法?

回答

2

如果您希望ItemContainer的模板控制前景,您可以将其设置为那里,而不是放在ItemTemplate中。 TextBlock.Foreground和TextBlock.FontSize可以用作附加属性和将从父值继承:

<ContentPresenter TextBlock.Foreground="White" TextBlock.FontSize="18"/> 

这些属性的设置将需要从ItemTemplate中除去,以避免重写继承的值。然后,您可以从VisualState动画中设置所需的任何值。

+0

虽然我不认为它适用于silverlight。 – AnthonyWJones 2010-11-22 10:13:48

+0

这是有效的!非常感谢约翰,但作为对此的延伸,我如何才能触发在不同视觉状态下改变这些值? – Mark 2010-11-22 21:59:53

+0

其实你可以:)我刚刚发现这个博客查尔斯http://www.charlespetzold.com/blog/2007/09/080231.html – Mark 2010-11-22 22:08:25

0

不要以为当前Silverlight版本中的答案已经解决了,ContentPresenter上的附加orpoerties甚至不再构建。任何人都知道这一点?

相关问题