WPF 数据模板的切换简单事例

  
    
< 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} " />

你可能感兴趣的:(WPF)