WPF 绑定:如何将文件路径列表中的名称绑定到 ListBox 中 TextBlock 的文本?

WPF Binding: How to bind a name from a list of filepaths to text of TextBlock in ListBox?

我正在尝试将文件路径给定的文件名绑定到 TextBlock。文件路径存储在一个列表中,该列表绑定到 ListBox 的 ItemsSourceProperty。 TextBlock 设置为 DataTemplate。 我的问题是:如何获取没有路径和扩展名的名称并将其绑定到 TextBlock?

更好解释的XAML代码:

<ListBox Name="MyListBox" Margin="2">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

后面的代码:

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
List<string> PathList = Directory.GetFiles(path, "*.txt").ToList();

Binding myBind = new Binding();
myBind.Source = PathList;

myListBox.SetBinding(ListBox.ItemsSourceProperty, myBind);

如果您只想向列表框中添加一个静态列表,您应该这样做。

XAML:

    <ListBox x:Name="lb" ItemsSource="{Binding Collection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

主window中的构造函数背后的代码:

        public MainWindow()
        {
            InitializeComponent();

            List<string> l = new List<string>();
            l.Add("string path 1");
            l.Add("string path 2");
            l.Add("string path 3");
            l.Add("string path 4");
            lb.ItemsSource = l;

        }

您应该知道有更好的方法来做这些事情。老实说,我建议你去看看 MVVM 并正确绑定到 ViewModel。

一个人使用转换器来更改选定列表框项目的文本,该项目的完整路径仅指向文件名。

在下面的示例中,有一个列表和旁边的一个文本框。选择项目后,绑定到列表 SelectedItem 的文本框会提取路径字符串,该字符串将传递给转换器,returns 只是要显示的文件名。

例子

XAML

<Window x:Class="WPFStack.ListBoxQuestions"
 xmlns:local="clr-namespace:WPFStack"
 xmlns:converters="clr-namespace:WPFStack.Converters"
.../>

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <converters:PathToFilenameConverter x:Key="FilenameConverter" />

        <x:Array x:Key="FileNames" Type="system:String">
            <system:String>C:\Temp\Alpha.txt</system:String>
            <system:String>C:\Temp\Beta.txt</system:String>
        </x:Array>

    </StackPanel.Resources>

    <ListBox  Name="lbFiles"
              ItemsSource="{StaticResource FileNames}" />

    <TextBlock Text="{Binding SelectedItem, 
                              ElementName=lbFiles,
                              Converter={StaticResource FilenameConverter}}"
                Margin="6,0,0,0" />

</StackPanel>

转换器

namespace WPFStack.Converters
{
public class PathToFilenameConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object result = null;

        if (value != null)
        {
            var path = value.ToString();

            if (string.IsNullOrWhiteSpace(path) == false)
                result = Path.GetFileNameWithoutExtension(path);
        }

        return result;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}
}

ItemTemplate转换器的使用

转换器在模板中重复使用

 <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource FilenameConverter}}"/>
        </DataTemplate>
  </ListBox.ItemTemplate>