2014-12-01 60 views
0

有人可以幫我嗎?我有如下因素XAML代碼我MainWindow.xaml文件:綁定列表<string>屬性到列表框WPF

 <ListBox ItemsSource="{Binding Files}" HorizontalAlignment="Left" 
       Height="371" Margin="281,53,0,0" VerticalAlignment="Top" 
       Width="609"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding}" /> 
        </StackPanel> 

       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

在我ViewModel.cs我有屬性:

public List<string> Files { get; set; } 

但是,當我按一下按鈕,並添加一些項目的文件什麼都沒發生。

P.S.對不起,我的英語不好:)

回答

1

這裏是您的解決方案,只需添加以下代碼,然後按「添加字符串」按鈕,使其工作。我用的 '的ObservableCollection',而不是名單,並使其使用 'INotifyPropertyChanged的' 接口ViewModel.cs類聽

MainWindow.xaml

<Window x:Class="ListBox_Strings.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:myApp="clr-namespace:ListBox_Strings" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <myApp:ViewModel/> 
    </Window.DataContext> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition Height="50"/> 
     </Grid.RowDefinitions> 
     <ListBox ItemsSource="{Binding Files}" HorizontalAlignment="Left" 
       VerticalAlignment="Top" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding}" /> 
        </StackPanel> 

       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <Button Grid.Row="1" Content="Add String" Click="Button_Click"></Button> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

using System.Windows; 
namespace ListBox_Strings 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      var vm = this.DataContext as ViewModel; 
      vm.Files.Add("New String"); 
     } 
    } 
} 

ViewModel.cs

using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace ListBox_Strings 
{ 
    public class ViewModel:INotifyPropertyChanged 
    { 
     private ObservableCollection<string> m_Files; 

     public ObservableCollection<string> Files 
     { 
      get { return m_Files; } 
      set { m_Files = value; 
       OnPropertyChanged("Files"); } 
     } 


     public ViewModel() 
     { 
      Files = new ObservableCollection<string>(); 
      Files.Add("A"); 
      Files.Add("B"); 
      Files.Add("C"); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string name) 
     { 
      if (PropertyChanged != null) 
      { 
       this.PropertyChanged(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
    } 
} 
+0

@Ihor,上述解決方案是否回答了您的查詢? – 2014-12-02 12:48:10

+0

我還沒試過呢。但謝謝你的回答。我會告訴你結果 – 2014-12-03 12:10:25