我需要根据所选项目的状态(选中或未选中)更改ListBox中项的DataTemplate (在选中时显示不同/更多信息)。
当单击相关的ListBox项(仅通过制表符),我无法在DataTemplate中的最上层元素(StackPanel)上获得GotFocus / LostFocus事件,我已经没有其他想法。
最简单的方法是提供“ItemContainerStyle”而不是“ItemTemplate”属性的模板。在下面的代码中,我创建了两个数据模板:一个用于“未选择”状态,另一个用于“已选择”状态。然后我创建了一个“ItemContainerStyle”的模板,它是包含项的实际“ListBoxItem”。我将默认的“ContentTemplate”设置为“Unselected”状态,然后提供一个触发器,在“IsSelected”属性为true时切换模板。(注意:出于简单起见,我在代码后台将“ItemsSource”属性设置为字符串列表)
<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}" />
要在选定或未选定项目时设置样式,您只需要在<DataTemplate>
中检索ListBoxItem
父级,并在其IsSelected
更改时触发样式更改。例如,下面的代码将创建一个带有默认Foreground
颜色为绿色的TextBlock
。现在,如果选择该项,则字体将变为红色,当鼠标悬停在该项上时,该项将变为黄色。这样,您无需为每种状态指定单独的数据模板(如其他答案所建议)就可以轻微更改。
<DataTemplate x:Key="SimpleDataTemplate">
<TextBlock Text="{Binding}">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.Foreground" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem }}}"
Value="True">
<Setter Property="TextBlock.Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem }}}"
Value="True">
<Setter Property="TextBlock.Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
需要注意的是,stackpanel无法获取焦点,因此它永远不会获得焦点(如果您真的想要它获得焦点,请设置Focusable=True)。但是,在这种情况下,需要记住的关键是Stackpanel是TreeViewItem的子项,而TreeViewItem是ItemContainer。正如Micah建议的那样,调整itemcontainerstyle是一个很好的方法。
您可以使用DataTemplates和datatriggers等内容来完成此操作,这些内容将使用RelativeSouce标记扩展来查找listviewitem。
BasedOn = "{StaticResource {x:Type ListBoxItem}}"
。这也适用于其他控件,如TreeView。 - Benny Jobigan