2011-04-21 112 views
0

全部。我有一個應用程序可以掃描圖片文件夾,並在列表框中顯示圖片及其名稱。每個圖像和圖像名稱(顯示在圖像旁邊的文本塊中)都存儲在列表框內的水平堆棧面板中。獲取選定列表框項目的名稱(WPF C#)?

我一直試圖整個下午找到一種方式,當用戶在列表框中選擇它時,在文本框中顯示圖像名稱。聽起來很簡單,我確定它是,但我似乎無法讓它工作。

任何人都可以指出我正確的方向嗎?謝謝。

這是我的XAML,如果有幫助:

<Grid> 

    <ListBox ItemsSource="{Binding AllImages}" Margin="0,0,262,0" Name="listBox1" MouseLeftButtonDown="listBox1_MouseLeftButtonDown"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="{Binding Image}" Width="50" Height="50" Margin="6"/> 
        <TextBlock Text="{Binding Name}" Margin="6" /> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox>   
    <TextBox Height="23" HorizontalAlignment="Left" Margin="265,148,0,0" Name="textBox1" VerticalAlignment="Top" Width="198" /> 
</Grid> 

而且我後面的代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    public class MyImage 
    { 
     private ImageSource _image; 
     private string _name; 

     public MyImage(ImageSource image, string name) 
     { 
      _image = image; 
      _name = name; 
     } 

     public override string ToString() 
     { 
      return _name; 
     } 

     public ImageSource Image 
     { 
      get { return _image; } 
     } 

     public string Name 
     { 
      get { return _name; } 
     } 
    } 

    public List<MyImage> AllImages 
    { 
     get 
     { 
      List<MyImage> result = new List<MyImage>(); 
      string filePath = @"D:\Projects\Visual Studio 2010\WpfApplication5\WpfApplication5\bin\Debug\ImageFolder"; 
      string[] files = Directory.GetFiles(filePath); 
      foreach (string filename in files) 
      { 
       try 
       { 
        result.Add(
        new MyImage(
        new BitmapImage(
        new Uri(filename)), 
        System.IO.Path.GetFileNameWithoutExtension(filename)));       
       } 
       catch { } 
      } 
      return result; 
     } 
    }   

} 

回答

0

看看這個問題。

How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?

在你的情況使用

<TextBox Height="23" 
    HorizontalAlignment="Left" 
    Margin="265,148,0,0" 
    Name="textBox1" 
    VerticalAlignment="Top" Width="198" 
    Text="{Binding SelectedItem.Name, ElementName=listBox1}"/> 
+0

這實際上是我認爲應該完成的,但我得到了異常錯誤:「TwoWay或OneWayToSource綁定無法在讀取-only property'Name',類型爲'WpfApplication5.MainWindow + MyImage'。「 - 如果有幫助,我已經在第一篇文章中列出了我的代碼。謝謝。 – SkullJacker 2011-04-21 14:42:47

+0

谷歌搜索會引導你到這:http://stackoverflow.com/questions/590269/a-twoway-or-onewaytosource-binding-cannot-work-on-the-read-only-property我期望這就是問題所在因爲您的文本框會允許用戶更改分配給它的值。無論如何,您可能想要使用某種標籤,除非您希望人們能夠更改名稱? – 2011-04-21 14:51:18

+0

啊,我明白了。非常感謝。我將它更改爲:Text =「{Binding SelectedItem.Name,ElementName = listBox1,Mode = OneWay}」,它完美地工作。謝謝你幫助我理解。 – SkullJacker 2011-04-21 14:56:02

0

從代碼中檢索選定的圖像,你至少有3個選項(我假設你的圖像是由一類ImageItem代表)

  • IsSynchronizedWithCurrentItem設置爲對您的ListBox爲真,並使用以下代碼檢索所選內容項目:

    ICollectionView view = CollectionViewSource(AllImages); 
    ImageItem selectedImage = (ImageItem)view.CurrentItem; 
    
  • 綁定ListBox的財產在你的DataContextSelectedItem

    <ListBox .... SelectedItem="{Binding SelectedImage}"> 
    
  • 訪問SelectedItem財產直接從後臺代碼:

    ImageItem selectedImage = (ImageItem)listBox1.SelectedItem; 
    

當然,如果你只是想要顯示名稱在TextBlock,你可以使用羅素特洛伊韋斯特的解決方案