2015-08-20 89 views
2

我試圖將由文件路徑給出的文件的名稱綁定到TextBlock。文件路徑存儲在綁定到ListBox的ItemsSourceProperty的列表中。 TextBlock被設置爲DataTemplate。 我的問題是:如何獲取沒有路徑和擴展名的名稱並將其綁定到TextBlock?WPF綁定:如何將文件路徑列表中的名稱綁定到ListBox中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

一個使用轉換器來將選中的列表框項目的文本完全改變爲文件名。

在下面的例子中,它旁邊有一個列表和一個文本框。選擇一個項目後,綁定到列表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> 
+0

真棒,正是我一直在尋找的,太感謝你了! –

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做適當的綁定。