1. <small id='Rs4zs'></small><noframes id='Rs4zs'>

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

      1. <legend id='Rs4zs'><style id='Rs4zs'><dir id='Rs4zs'><q id='Rs4zs'></q></dir></style></legend>
      2. <tfoot id='Rs4zs'></tfoot>
      3. 制作 WPF ListBox 逗号分隔值

        Make a WPF ListBox comma separate values(制作 WPF ListBox 逗号分隔值)

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

          • <bdo id='Ce5qK'></bdo><ul id='Ce5qK'></ul>
          • <legend id='Ce5qK'><style id='Ce5qK'><dir id='Ce5qK'><q id='Ce5qK'></q></dir></style></legend>
                  <tbody id='Ce5qK'></tbody>
              • <small id='Ce5qK'></small><noframes id='Ce5qK'>

                1. 本文介绍了制作 WPF ListBox 逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个如下所示的 ListBox:

                  I have a ListBox that looks like this:

                  <ListBox ItemsSource="{Binding Fruits}">
                      <!--Make the items wrap--> 
                      <ListBox.ItemsPanel>
                          <ItemsPanelTemplate>
                              <WrapPanel></WrapPanel>
                          </ItemsPanelTemplate>
                      </ListBox.ItemsPanel>
                      <ListBox.ItemTemplate>
                          <DataTemplate>
                              <TextBlock Text="{Binding Name, StringFormat=' {0},'}" />
                          </DataTemplate>
                      </ListBox.ItemTemplate>
                  </ListBox>
                  

                  这给了我一个这样的列表:

                  This gives me a list like this:

                  橙子、葡萄、香蕉、

                  但我想要的是:

                  橙子、葡萄、香蕉

                  (无尾随逗号)

                  有人知道如何删除尾随逗号吗?

                  推荐答案

                  这可以使用 IValueConverter 通过使用 XAML 中的数据触发器更新绑定上的 StringFormat 来确定其 listBox 中的最后一项是否存在.

                  This can be achieved using IValueConverter to determine whether its last item in a listBox and there by updating StringFormat on your binding using data trigger in XAML.

                  创建一个转换器来确定值是否是列表框中的最后一项 -

                  Create a converter to determine if value is last item in listbox -

                  public class IsLastItemInContainerConverter : IValueConverter
                  {
                      public object Convert(object value, Type targetType, object parameter,
                                            CultureInfo culture)
                      {
                          DependencyObject item = (DependencyObject)value;
                          ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
                  
                          return ic.ItemContainerGenerator.IndexFromContainer(item) ==
                                                                          ic.Items.Count - 1;
                      }
                  
                      public object ConvertBack(object value, Type targetType, object parameter,
                                                CultureInfo culture)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  现在修改您的 XAML 并在 DataTemplate 上添加触发器以从 TextBlock 上的 StringFormat 中删除逗号 -

                  Now modify your XAML and add trigger on DataTemplate to remove comma from StringFormat on your TextBlock -

                  <ListBox ItemsSource="{Binding Fruits}">
                     <ListBox.Resources>
                        <local:IsLastItemInContainerConverter
                               x:Key="IsLastItemInContainerConverter"/>
                     </ListBox.Resources>
                     <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                           <WrapPanel/>
                        </ItemsPanelTemplate>
                     </ListBox.ItemsPanel>
                     <ListBox.ItemTemplate>
                        <DataTemplate>
                           <TextBlock x:Name="textBlock"
                                      Text="{Binding Name, StringFormat=' {0},'}" />
                              <DataTemplate.Triggers>
                                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                                                                 Mode=FindAncestor, 
                                                                 AncestorType=ListBoxItem},
                                                                 Converter={StaticResource IsLastItemInContainerConverter}}" 
                                               Value="True">
                                      <Setter Property="Text" TargetName="textBlock"
                                              Value="{Binding Name, StringFormat=' {0}'}"/>
                                   </DataTrigger>
                              </DataTemplate.Triggers>
                         </DataTemplate>
                     </ListBox.ItemTemplate>
                  </ListBox>
                  

                  这篇关于制作 WPF 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 列表框上的取消选择)
                    <i id='F6YGI'><tr id='F6YGI'><dt id='F6YGI'><q id='F6YGI'><span id='F6YGI'><b id='F6YGI'><form id='F6YGI'><ins id='F6YGI'></ins><ul id='F6YGI'></ul><sub id='F6YGI'></sub></form><legend id='F6YGI'></legend><bdo id='F6YGI'><pre id='F6YGI'><center id='F6YGI'></center></pre></bdo></b><th id='F6YGI'></th></span></q></dt></tr></i><div id='F6YGI'><tfoot id='F6YGI'></tfoot><dl id='F6YGI'><fieldset id='F6YGI'></fieldset></dl></div>
                    • <bdo id='F6YGI'></bdo><ul id='F6YGI'></ul>
                      <legend id='F6YGI'><style id='F6YGI'><dir id='F6YGI'><q id='F6YGI'></q></dir></style></legend>

                          <tfoot id='F6YGI'></tfoot>
                            <tbody id='F6YGI'></tbody>

                          1. <small id='F6YGI'></small><noframes id='F6YGI'>