如何使第一项在ListBox中以粗体显示? [英] How to have first item bolded in ListBox?

查看:210
本文介绍了如何使第一项在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>

,但是我需要能够在运行时更改文本绑定路径(NameEnglishNameRomanian).因此,我尝试在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>

这篇关于如何使第一项在ListBox中以粗体显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆