2014-12-01 70 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