文件名字符串转换器的文件路径不起作用 [英] File path to file name String converter not working

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

问题描述

使用 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>

这篇关于文件名字符串转换器的文件路径不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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