如何使第一项在ListBox中以粗体显示?
我有一个ListBox,我只想将第一项加粗.
I have a ListBox in which I would like to have only the first item bolded.
查看:
<ListBox x:Name="lstBox" ItemsSource="{Binding List}" DisplayMemberPath="{Binding SequencesDisplayLanguage}" />
Viewmodel:
Viewmodel:
private ObservableCollection<Sequence> _list = new ObservableCollection<Sequence>() { };
public ObservableCollection<Sequence> List { get { return _list; } }
private string _sequencesDisplayLanguage = "NameEnglish";
public string SequencesDisplayLanguage
{
get
{
return _sequencesDisplayLanguage;
}
set
{
_sequencesDisplayLanguage = value;
OnPropertyChanged("SequencesDisplayLanguage");
}
}
型号:
public class Sequence : INotifyPropertyChanged
{
public Sequence()
{
NameEnglish = "";
NameRomanian = "";
}
private string _nameEnglish;
public string NameEnglish
{
get
{
return _nameEnglish;
}
set
{
_nameEnglish = value;
OnPropertyChanged("NameEnglish");
}
}
private string _nameRomanian;
public string NameRomanian
{
get
{
return _nameRomanian;
}
set
{
_nameRomanian = value;
OnPropertyChanged("NameRomanian");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我尝试过使用ItemTemplate,如果该项目属于特定类型,则该转换器会返回FontWeights.Bold
(我已经小心地将该特定项目放在列表中,因此它将以粗体显示).代码是这样的:
I've tried using a ItemTemplate, with a converter which returns FontWeights.Bold
if the item is of a particular type (I've taken care to put that particular item first in the list so it will be bolded). The code is this:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontWeight="{Binding Converter={StaticResource sequenceToFontWeightConverter}}"
Text="{Binding Path=NameEnglish}" />
</DataTemplate>
</ListBox.ItemTemplate>
,但是我需要能够在运行时更改文本绑定路径(NameEnglish
或NameRomanian
).因此,我尝试在Viewmodel中引用该属性:
but I need to be able to change the text binding path at runtime (NameEnglish
or NameRomanian
). So I've tried referencing the property in the Viewmodel:
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.SequencesDisplayLanguage}"/>
,但是它不起作用(如果SequencesDisplayLanguage=="NameEnglish"
,则所有ListBox项都显示为"NameEnglish").
but it doesn't work (if SequencesDisplayLanguage=="NameEnglish"
then all ListBox items show up as "NameEnglish").
因此,如何在运行时更改绑定路径的同时,我只能加粗ListBox中的第一项?
更新
我尝试了Clemens的解决方案,但是现在选中的项目突出显示已更改:该项目具有更高的高度,选择时会出现带有边框和不同颜色的矩形(参见图片).
I tried Clemens' solution, but now the selected item highlighting has changed: the item has a larger height, a rectangle with border and different color appears when selecting (see picture).
如何保持原始项目的大小和突出显示?
更新2
发现:
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
您可以通过在AlternationIndex
附加属性上设置Trigger
来根据其索引为ListBoxItem
设置样式.您还必须为AlternationCount
属性设置足够大的值:
You could style a ListBoxItem
based on its index by setting a Trigger
on its AlternationIndex
attached property. You would also have to set a large enough value for the AlternationCount
property:
<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>