C# 如何在列表框中以粗体显示第一项?
我有一个列表框,我只想有第一个项目加粗 视图: 我尝试过使用ItemTemplate,它带有一个返回FontWeights.Bold的转换器。如果项目是特定类型的,我会注意将该特定项目放在列表的第一位,以便将其加粗。代码如下:C# 如何在列表框中以粗体显示第一项?,c#,wpf,xaml,mvvm,listbox,C#,Wpf,Xaml,Mvvm,Listbox,我有一个列表框,我只想有第一个项目加粗 视图: 我尝试过使用ItemTemplate,它带有一个返回FontWeights.Bold的转换器。如果项目是特定类型的,我会注意将该特定项目放在列表的第一位,以便将其加粗。代码如下: <ListBox.ItemTemplate> <DataTemplate> <TextBlock FontWeight="{Binding Converter={StaticResource sequenceToFontWeigh
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontWeight="{Binding Converter={StaticResource sequenceToFontWeightConverter}}"
Text="{Binding Path=NameEnglish}" />
</DataTemplate>
</ListBox.ItemTemplate>
但是如果SequencesDisplayLanguage==NameEnglish,那么所有列表框项都显示为NameEnglish,则它不起作用
那么,如何在运行时更改绑定路径的同时仅加粗列表框中的第一项
更新
我尝试了克莱门斯的解决方案,但现在选中的项目高亮显示已发生变化:项目具有更大的高度,选择“查看图片”时会出现一个带边框和不同颜色的矩形
如何保持原始项目大小和突出显示
更新2
发现:
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
您是如何订购列表以确保您想要的物品是第一位的?ObservableCollection中项目的顺序就是它们添加的顺序
如果列表成员的属性为序列号,则只需将转换器更改为绑定到该属性,如果序列号为0,则返回粗体,否则返回普通。如何对列表进行排序,以确保所需项目位于第一位?ObservableCollection中项目的顺序就是它们添加的顺序 如果列表成员的属性为序列号,则只需将转换器更改为绑定到该序列号,如果序列号为0,则返回粗体,否则返回普通。可以通过在其AlternationalIndex附加属性上设置触发器,根据其索引设置ListBoxItem的样式。您还必须为AlternationCount属性设置足够大的值:
<ListBox ItemsSource="{Binding List}" AlternationCount="2147483647">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
通过在其AlternationIndex附加属性上设置触发器,可以根据其索引设置ListBoxItem的样式。您还必须为AlternationCount属性设置足够大的值:
<ListBox ItemsSource="{Binding List}" AlternationCount="2147483647">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
抱歉,事实上它是一个ObservableCollectionEx,扩展了排序的可能性,但我使用ObservableCollection是为了避免使代码理解复杂化。我没有序列号属性,但感谢您的想法。对不起,事实上,它是一个ObservableCollectionEx,扩展了排序的可能性,但我使用ObservableCollection是为了避免使代码理解复杂化。我没有序列号属性,但是谢谢你的想法。谢谢你的解决方法。但是,所选项目突出显示已更改,请参见更新的答案。您知道如何保持原始行为吗?发现:@CristianM没有必要将ListBoxItem样式显式地基于默认的ListBoxItem样式。默认情况下会发生这种情况。你可能有其他的ListBoxItem样式吗?没有,我没有定义其他样式。可能因为我使用的是MahApps,所以它可能包含ListBoxItem样式吗?谢谢您提供了很好的解决方法。但是,所选项目突出显示已更改,请参见更新的答案。您知道如何保持原始行为吗?发现:@CristianM没有必要将ListBoxItem样式显式地基于默认的ListBoxItem样式。默认情况下会发生这种情况。你可能有其他的ListBoxItem样式吗?没有,我没有定义其他样式。可能是因为我使用的是MahApps,它会包含ListBoxItem样式吗?
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.SequencesDisplayLanguage}"/>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<ListBox ItemsSource="{Binding List}" AlternationCount="2147483647">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>