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

3
我将尝试将由文件路径给出的文件名绑定到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);
2个回答

4

使用转换器将选定的列表框项的文本从完整路径更改为仅文件名。

在以下示例中,有一个列表和一个紧挨着它的文本框。一旦选择了项目,绑定到列表的SelectedItem的文本框会提取传递给转换器的路径字符串,该转换器返回要显示的文件名。

示例

enter image description here

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>

太棒了,正是我在寻找的,非常感谢你! - Steffen Gerdes

1
如果您只想将静态列表添加到列表框中,应该这样做。 XAML:
    <ListBox x:Name="lb" ItemsSource="{Binding Collection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

主窗口中构造函数的代码:

        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绑定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接