<Window x:Class="MVVM.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MVVM" mc:Ignorable="d" Title="MainWindow" Height="500" Width="800"> <Window.Resources> <Style x:Key="stackStyle" TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Age}" Value="0"> <Setter Property="Background" Value="Yellow"></Setter> </DataTrigger> </Style.Triggers> </Style> <DataTemplate x:Key="personTemplete"> <StackPanel Margin="2" Orientation="Horizontal" Style="{StaticResource stackStyle}"> <TextBlock Text="{Binding Name}" Width="50"></TextBlock> <TextBlock Text="{Binding Age}"></TextBlock> </StackPanel> </DataTemplate> </Window.Resources> <Grid> <ListView x:Name="listView" Margin="60" ItemsSource="{Binding PersonList}" ItemTemplate="{StaticResource personTemplete}"></ListView> <Button Height="25" Width="75" Command="{Binding AddRowCommand}" HorizontalAlignment="Left" Margin="10,0,10,10" VerticalAlignment="Bottom" Content="加一行"></Button> </Grid> </Window>
这是后台.cs代码:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); } }
ViewModelBase类:
public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; //当属性改变的时候,调用该方法来发起一个消息,通知View中绑定了propertyName的元素做出调整 public void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
MainWindowViewModel:
public class MainWindowViewModel :ViewModelBase { public ObservableCollection<Person> PersonList = new ObservableCollection<Person>(); //public Person Person = new Person { Name = "test", Age = 31 }; public MainWindowViewModel() { this.PersonList.Add(new MVVM.Person { Name = "trent", Age = 44 }); this.PersonList.Add(new MVVM.Person { Name = "kurt", Age = 23 }); this.PersonList.Add(new MVVM.Person { Name = "denny", Age = 30 }); } private ICommand addRowCommand; public ICommand AddRowCommand { get { if (this.addRowCommand == null) this.addRowCommand = new DelegateCommand(x => this.AddRow()); return this.addRowCommand; } } private void AddRow() { this.PersonList.Add(new MVVM.Person { Name = "eysa", Age = 31 }); } }
DelegateCommand类:
public class DelegateCommand : ICommand { readonly Action<object> _execute; readonly Predicate<object> _canExecute; public DelegateCommand(Action<object> execute) : this(execute, null) { } public DelegateCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } public void Execute(object parameter) { _execute(parameter); } public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } }
我看别人博客里写的就是这样的,但是我为什么跑起来没绑定上PersonList呢?
问题出在PersonList不是属性,只是一个字段。
数据源和你绑定的不在同一级别