C# 格式可以在XAML中显示路径字符串的文件名吗

C# 格式可以在XAML中显示路径字符串的文件名吗,c#,wpf,xaml,string.format,C#,Wpf,Xaml,String.format,嗨,我是C#和WPF的新手。以下只是未经测试的示例 假设我有 String path1 = "C:\Users\user\Desktop\File.ext" String path2 = "C:\temp\text.txt" String.Format(path1.Substring(path1.LastIndexOf('\')+1)) String.Format(path2.Substring(path1.LastIndexOf('\')+1)) 我希望在不更改原始字符串的情况下从路径中获

嗨,我是C#和WPF的新手。以下只是未经测试的示例

假设我有

String path1 = "C:\Users\user\Desktop\File.ext"
String path2 = "C:\temp\text.txt"

String.Format(path1.Substring(path1.LastIndexOf('\')+1))
String.Format(path2.Substring(path1.LastIndexOf('\')+1))
我希望在不更改原始字符串的情况下从路径中获取文件名,并且我希望最好在XAML中这样做,或者使用值转换器

我正在研究这个问题,想知道是应该在一个可观察的集合和ListView之间进行值转换,还是只使用两个列表。一个是手动在模型中包含文件路径,另一个是在ListView中显示文件名,并分别进行更新(当前方法)

我的数据模型

private List<String> AttachedFiles;
public ObservableCollection<String> fileNames { get; set; }

    public void addFilePath(String filePath) {
        this.AttachedFiles.Add(filePath);
    }

    public void removeFilePath(String filePath)
    {
        this.AttachedFiles.Remove(filePath);
    }

使用一个集合,不要使用
列表

并使用
数据模板中的值转换器。由于
文件路径
字符串
的集合,因此
数据模板
数据上下文
将是一个
字符串
,因此绑定不会指定绑定要使用的属性。传递给值转换器的值将是字符串本身

<ListView x:Name="DropList" ItemsSource="{Binding FilePaths}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Label 
                Content="{Binding Converter={local:FileName}}" 
                />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

如果您在这里只处理一个属性,您可以按照其他答案的建议使用转换器。但是您也可以为
Path
对象创建一个新类,并在此类中定义
FilePath
string属性来存储完整路径。(如果需要,还可以提供更多属性。)

然后您可以在这个类中重写
ToString
方法,只返回
文件名

通过这样做,您不需要更改XAML,但绑定列表将只显示该项作为覆盖
ToString
方法返回的结果

比如:

public ObservableCollection<MyPath> fileNames { get; set; }


public class MyPath: ObservableObject  //implement INotifyPropertyChanged
{
    private string _filepath;        
    public string FilePath
    {
        get { return _filepath; }
        set
        {
            if (value != _filepath)
            {
                _filepath= value;
                RaisePropertyChanged("FilePath");
            }
        }
    }
    public override string ToString()
    {
        return System.IO.Path.GetFileName(FilePath);
    }
}
publicobservableCollection文件名{get;set;}
公共类MyPath:ObservableObject//implement INotifyPropertyChanged
{
私有字符串\u文件路径;
公共字符串文件路径
{
获取{return\u filepath;}
设置
{
如果(值!=\u文件路径)
{
_filepath=value;
RaisePropertyChanged(“文件路径”);
}
}
}
公共重写字符串ToString()
{
返回System.IO.Path.GetFileName(FilePath);
}
}

请参见标记的工作代码副本,以仅显示文件名。有很多方法可以解决这个问题。如果您觉得标记的副本无法解决您的问题,请发布一个新问题,其中包括清楚显示您已尝试的内容,并具体说明您在工作中遇到的困难。不幸的是,它不起作用,exemel给出了一个错误,它期望IValueConverter类型xaml是:@RobertMazurowski local:Ticket的类定义是什么样子的?它是这个类MyModel:MarkupExtension,IValueConverter,INotifyPropertyChanged。我的类构造函数正在创建列表:public myModel(){this.fileNames=new observeCollection();}@RobertMazurowski为什么要实现INotifyPropertyChanged?试着把它去掉。我用这种方式编写了很多值转换器,但从来没有遇到过问题。@RobertMazurowski,等等:那是一个名为Ticket的类吗?还是我的模特?这张票是什么样子的?哪一个给了你错误?
ObservableCollection<String> FilePaths { get; private set; }
public class FileName : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider) => this;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.IO.Path.GetFileName(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<ListView x:Name="DropList" ItemsSource="{Binding FilePaths}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Label 
                Content="{Binding Converter={local:FileName}}" 
                />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public ObservableCollection<MyPath> fileNames { get; set; }


public class MyPath: ObservableObject  //implement INotifyPropertyChanged
{
    private string _filepath;        
    public string FilePath
    {
        get { return _filepath; }
        set
        {
            if (value != _filepath)
            {
                _filepath= value;
                RaisePropertyChanged("FilePath");
            }
        }
    }
    public override string ToString()
    {
        return System.IO.Path.GetFileName(FilePath);
    }
}