2011-12-27 68 views
0

不能得到任何数据和数据绑定工作,我有inotify的事件,我有在XAML对象的绑定,但没有显示出来,如果我改变了标贴的内容,以“东西”它作品,但没有说明负载或点击我的按钮数据绑定无法与视图模型工作

我的XAML视图

<Grid> 
    <StackPanel Name="stackpanel"> 
     <Label Content="{Binding Name}" /> 
     <Label Content="{Binding Length}" /> 
     <Label Content="{Binding Rating}" /> 
     <Button Content="Change text" Click="ButtonClick" /> 
    </StackPanel> 
</Grid> 

它的代码隐藏

public partial class Movie 
{ 
    readonly MovieViewModel _movieViewModel; 

    public Movie() 
    { 
     InitializeComponent(); 
     _movieViewModel = new MovieViewModel { Movie = { Name = "The Dark Knight", Length = 180, Rating = 88 } }; 
     stackpanel.DataContext = _movieViewModel; 
    } 

    private void ButtonClick(object sender, RoutedEventArgs e) 
    { 
     _movieViewModel.Movie.Name = "bad movie"; 
    } 
} 

视图模型

class MovieViewModel 
{ 
    public MovieViewModel() : this(new Movie()) 
    { 
    } 

    public MovieViewModel(Movie movie) 
    { 
     Movie = movie; 
    } 

    public Movie Movie { get; set; } 
} 

示范

class Movie : INotifyPropertyChanged 
{ 
    public Movie() 
    {} 

    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      NotifyPropertyChanged("Name"); 
     } 
    } 

    private int _length; 
    public int Length 
    { 
     get { return _length; } 
     set 
     { 
      _length = value; 
      NotifyPropertyChanged("Length"); 
     } 
    } 

    private int _rating; 
    public int Rating 
    { 
     get { return _rating; } 

     set 
     { 
      if (_rating == value) return; 
      _rating = value; 
      NotifyPropertyChanged("_Rating"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

回答

3

你必须设置您的绑定错误,这就是正在显示什么原因。

只要看看在你ViewModel,比上绑定一探究竟。您尝试绑定到名为Name的属性,但您的MovieViewModel不公开任何具有该名称的属性。我非常确定向您报告了绑定错误(在输出窗口中查看消息)。

要使其工作,你需要无论是在你的ViewModel公开属性,以匹配您尝试绑定到(坏)中的那些,或更改绑定在你的xaml有正确的路径:

<Label Content="{Binding Movie.Name}" /> 
<Label Content="{Binding Movie.Length}" /> 
<Label Content="{Binding Movie.Rating}" /> 

这应该让你走吧。

此外 - 您可能要实现INotifyPropertyChanged还您MovieViewModel类,如果您打算更改分配给Movie财产Movie对象。只要你改变属性Movie已经分配给MovieViewModel的对象,一切都会好的,但是如果你想改变分配给这个属性的实际对象,不会产生更改通知,你的UI将不能正常工作。

而且 - 我注意到,你做你的NotifyPorpertyChanged方法公开 - 我不会建议这是任何人现在可以触发该事件。通常的做法是将这些方法设置为私有或保护,这取决于您是否想通过继承类来提供触发事件的方式(这很可能发生在PropertyChanged事件的情况下)。

1

我认为你有一个打字错误

NotifyPropertyChanged("_Rating"); 

应该

NotifyPropertyChanged("Rating"); 
0

而不是使用标签,我会建议你使用Texblock 。试试下面的代码

_movieViewModel = new MovieViewModel 
         { Movie = { Name = "The Dark Knight", Length = 180, Rating = 88 } }; 
this.DataContext = _movieViewModel; 

文字块像下面

<StackPanel Name="stackpanel"> 
    <TextBlock Name="textBlock1" Text="{Binding Path=Name}"/> 
    <TextBlock Name="textBlock2" Text="{Binding Path=Length}"/> 
    <Button Content="Change text" Click="ButtonClick" /> 
</StackPanel> 
相关问题