如何在metro应用程序中使用DataTemplateKey

时间:2012-04-08 17:12:52

标签: xaml microsoft-metro

新的Windows 8 metro API仍然定义了类DataTemplateKey。

但是,我可以弄清楚如何使用它。

您是否在XAML中有一个示例,说明如何使用它?

1 个答案:

答案 0 :(得分:0)

以下是有关如何使用DataTemplateKey的示例。只需注释和提醒: x:Key属性优先于基于DataType生成的自动DataTemplateKey

<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
    <TextBlock Text="{Binding}" Foreground="Red" />
</DataTemplate>

<DataTemplate x:Key="SelectedTemplate">
    <TextBlock Text="{Binding}" Foreground="White" />
</DataTemplate>

<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
    <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
        </Trigger>
    </Style.Triggers>
</Style>

</Window.Resources>
<ListBox x:Name="lstItems" ItemContainerStyle="{StaticResource ContainerStyle}" />
相关问题