<small id='LYOuh'></small><noframes id='LYOuh'>

      • <bdo id='LYOuh'></bdo><ul id='LYOuh'></ul>
      <legend id='LYOuh'><style id='LYOuh'><dir id='LYOuh'><q id='LYOuh'></q></dir></style></legend>

    1. <i id='LYOuh'><tr id='LYOuh'><dt id='LYOuh'><q id='LYOuh'><span id='LYOuh'><b id='LYOuh'><form id='LYOuh'><ins id='LYOuh'></ins><ul id='LYOuh'></ul><sub id='LYOuh'></sub></form><legend id='LYOuh'></legend><bdo id='LYOuh'><pre id='LYOuh'><center id='LYOuh'></center></pre></bdo></b><th id='LYOuh'></th></span></q></dt></tr></i><div id='LYOuh'><tfoot id='LYOuh'></tfoot><dl id='LYOuh'><fieldset id='LYOuh'></fieldset></dl></div>

        <tfoot id='LYOuh'></tfoot>

      1. 如何在 ListBox 中将第一项加粗?

        How to have first item bolded in ListBox?(如何在 ListBox 中将第一项加粗?)

        <tfoot id='92KaM'></tfoot>

            <bdo id='92KaM'></bdo><ul id='92KaM'></ul>
            <i id='92KaM'><tr id='92KaM'><dt id='92KaM'><q id='92KaM'><span id='92KaM'><b id='92KaM'><form id='92KaM'><ins id='92KaM'></ins><ul id='92KaM'></ul><sub id='92KaM'></sub></form><legend id='92KaM'></legend><bdo id='92KaM'><pre id='92KaM'><center id='92KaM'></center></pre></bdo></b><th id='92KaM'></th></span></q></dt></tr></i><div id='92KaM'><tfoot id='92KaM'></tfoot><dl id='92KaM'><fieldset id='92KaM'></fieldset></dl></div>
          • <small id='92KaM'></small><noframes id='92KaM'>

              <tbody id='92KaM'></tbody>
            <legend id='92KaM'><style id='92KaM'><dir id='92KaM'><q id='92KaM'></q></dir></style></legend>

                • 本文介绍了如何在 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 中将第一项加粗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <legend id='oBu7P'><style id='oBu7P'><dir id='oBu7P'><q id='oBu7P'></q></dir></style></legend>

                  <small id='oBu7P'></small><noframes id='oBu7P'>

                    <tbody id='oBu7P'></tbody>

                      <i id='oBu7P'><tr id='oBu7P'><dt id='oBu7P'><q id='oBu7P'><span id='oBu7P'><b id='oBu7P'><form id='oBu7P'><ins id='oBu7P'></ins><ul id='oBu7P'></ul><sub id='oBu7P'></sub></form><legend id='oBu7P'></legend><bdo id='oBu7P'><pre id='oBu7P'><center id='oBu7P'></center></pre></bdo></b><th id='oBu7P'></th></span></q></dt></tr></i><div id='oBu7P'><tfoot id='oBu7P'></tfoot><dl id='oBu7P'><fieldset id='oBu7P'></fieldset></dl></div>
                          <tfoot id='oBu7P'></tfoot>

                            <bdo id='oBu7P'></bdo><ul id='oBu7P'></ul>