2011-09-04 34 views

回答

3
<ListBox ItemsSource={Binding ViewModelPropertyName}" /> 

ViewModelPropertyName应返回IList或更好。
如果要显示集合的更改,则应返回INotifyCollectionChanged,如ObservableCollection<T>

<ListBox ItemsSource={Binding ViewModelPropertyName}" /> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text={Binding PropertyNameWithinObject} /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

谢谢你的回复。我找到了你写的一个样本。我会试试看。如果您知道的话,请让我知道好样品。 –

+0

什么样的样本? – SLaks

0
在同上面的例子

,需要在类定义的结合名称“ViewModelPropertyname”

示例:类名称为“模型”

 int _PropertyNameWithinObject; 
     public int PropertyNameWithinObject 
     { 
      get 
      { 
       return PropertyNameWithinObject; 
      } 
      set 
      { 
       PropertyNameWithinObject= value; 
       OnPropertyChanged("PropertyNameWithinObject"); 
      } 
     } 

包括关于以下类class“Model”

public class ViewModelBaseEx : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
    } 

然后在另一个类中定义集合,命名为“Vi ewModel”

 ObservableCollection<Model> _ViewModelPropertyName= new ObservableCollection<Model>(); 
     public ObservableCollection<Model> ViewModelPropertyName 
     { 
      get 
      { 
       return _ViewModelPropertyName; 
      } 
      set 
      { 
       _ViewModelPropertyName= value; 
       OnPropertyChanged("ViewModelPropertyName"); 
      } 
     } 

以下绑定

<ListBox ItemsSource={Binding ViewModelPropertyName}" /> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text={Binding PropertyNameWithinObject} /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

,并指定类 “在C#或设计视图模型” 中的datacontext此页面,在这里,我在C#页面中声明, 像

this.DataContext = ViewModel;