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

    <tfoot id='k3eOR'></tfoot>
    • <bdo id='k3eOR'></bdo><ul id='k3eOR'></ul>

  2. <small id='k3eOR'></small><noframes id='k3eOR'>

      文件名字符串转换器的文件路径不起作用

      File path to file name String converter not working(文件名字符串转换器的文件路径不起作用)
        <bdo id='jy53W'></bdo><ul id='jy53W'></ul>
        <tfoot id='jy53W'></tfoot>
      • <legend id='jy53W'><style id='jy53W'><dir id='jy53W'><q id='jy53W'></q></dir></style></legend>

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

              • <small id='jy53W'></small><noframes id='jy53W'>

                本文介绍了文件名字符串转换器的文件路径不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                使用 wpf ListBox 我试图显示文件名列表而不显示完整路径(对用户来说更方便).

                Using a wpf ListBox I'm trying to display a list of filename without displaying the full path (more convenient for user).

                数据来自一个使用 Dialog 填充的 ObservableCollection.

                Data comes from an ObservableCollection which is filled using Dialog.

                    private ObservableCollection<string> _VidFileDisplay = new ObservableCollection<string>(new[] {""});
                
                    public ObservableCollection<string> VidFileDisplay
                    {
                        get { return _VidFileDisplay; }
                        set { _VidFileDisplay = value; }
                    }
                

                最后我想选择一些项目并取回完整的文件路径.为此,我有一个转换器:

                In the end I want to select some items and get back the full file path. For this I have a converter :

                  public class PathToFilenameConverter : IValueConverter
                  {
                      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                      {
                          //return Path.GetFileName(value.ToString());
                          string result = null;
                          if (value != null)
                          {
                              var path = value.ToString();
                
                              if (string.IsNullOrWhiteSpace(path) == false)
                                  result = Path.GetFileName(path);
                          }
                          return result;
                      }
                
                      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                      {
                          return value;
                      }
                  }
                

                我绑定到我的列表框项目源:

                Which I bind to my listbox itemsource :

                <ListBox x:Name="VideoFileList" Margin="0" Grid.Row="1" Grid.RowSpan="5" Template="{DynamicResource BaseListBoxControlStyle}" ItemContainerStyle="{DynamicResource BaseListBoxItemStyle}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=DataContext.VidFileDisplay, Converter={StaticResource PathToFileName},ElementName=Ch_Parameters, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=SelectedVidNames,ElementName=Ch_Parameters, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                

                没有转换器,它工作正常(当然这是列表框中显示的完整路径).使用转换器,我每行一个字符......显示这个:

                Without the converter, it is working fine (but of course this is the full path displayed in the listbox). With the converter I have one character per line... displaying this :

                System.Collections.ObjectModel.ObservableCollection`1[System.String]
                

                我哪里错了?

                谢谢

                推荐答案

                ItemsSource 中绑定转换器适用于整个列表,而不适用于集合中的每个项目.如果你想为每个项目应用你的转换器,你需要这样做 ItemTemplate

                In ItemsSource binding converter applies to the whole list and not to each item in the collection. If you want to apply your converter per item you need to do it ItemTemplate

                <ListBox x:Name="VideoFileList" ItemsSource="{Binding Path=DataContext.VidFileDisplay, ElementName=Ch_Parameters}" ...>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=., Converter={StaticResource PathToFileName}}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </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 列表框上的取消选择)
                  • <small id='Kh53X'></small><noframes id='Kh53X'>

                  • <legend id='Kh53X'><style id='Kh53X'><dir id='Kh53X'><q id='Kh53X'></q></dir></style></legend>

                        <bdo id='Kh53X'></bdo><ul id='Kh53X'></ul>
                            <tbody id='Kh53X'></tbody>

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