如何在 ListBox 中将第一项加粗? [英] How to have first item bolded in ListBox?

查看:17
本文介绍了如何在 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}" />

视图模型:

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天全站免登陆