XAML绑定不反映ViewModel中的更改

时间:2014-12-24 01:26:01

标签: c# wpf xaml mvvm

我有以下代码:

MoviePanel.xaml

<StackPanel Orientation="Horizontal" >
        <Border BorderThickness="3" BorderBrush="Black">
            <DockPanel Width="350">
                <StackPanel  VerticalAlignment="Top" Height="200">
                    <TextBlock Width="200" Height="50" DockPanel.Dock="Top" HorizontalAlignment="Left">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} ({1})">
                                <Binding Path = "_selectedMovie.title"/>
                                <Binding Path = "_selectedMovie.year"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
                <StackPanel VerticalAlignment="Top" Height="200">
                    <Grid HorizontalAlignment="Center">
                        <Image Source="star_icon.png" Width="100"/>
                        <TextBlock Text="{Binding _selectedMovie.rating}" Style="{StaticResource AnnotationStyle}" Width="150"/>
                    </Grid>
                </StackPanel>
            </DockPanel>
        </Border>
</StackPanel>

MoviePanel.xaml.cs

public MoviePanel()
    {
        InitializeComponent();
        _moviePanelVM = new MoviePanelViewModel();
        _moviePanelVM.GetAllMovieDetails();            
        this.DataContext = _moviePanelVM;            
    }

MoviePanelViewModel.cs

 public class MoviePanelViewModel : INotifyPropertyChanged
 {
    // List to hold movieDetail objects 
    public List<MediaDetail> _movies { get; set; }

    public MediaDetail _selectedMovie { get; set; }

    public string selectedMovieID { get; set; }

    GetSelectedMovieDetails()
    {
       // Set _selectedMovie here
    }

MediaDetail

public class MediaDetail : DependencyObject {

    public ImageSource image {get; set;}        
    public string title {get; set;}
    public string year { get; set; }
    public string id { get; set; }
    public string rating { get; set; }
    public string description { get; set; }
    public string[] cast{ get; set; }
    public string[] director { get; set; }
    public string[] genre { get; set; }
    public string votecount { get; set; }
    public string[] language { get; set; }
    public string runtime { get; set; }
    public string part { get; set; }
    public string tagline { get; set; }

    public bool isMovieSelected
    {
        get { return (bool)this.GetValue(isMovieSelectedProperty); }
        set
        {
            this.SetValue(isMovieSelectedProperty, value);
        }
    }
    public static readonly DependencyProperty isMovieSelectedProperty = DependencyProperty.Register(
                                                             "isMovieSelected", typeof(bool),
                                                             typeof(MediaDetail));

}

我遇到的问题是我的UI没有更新。这是_selectedMovie.title_selectedMovie.year_selectedMovie.rating未更新。

XAML的另一部分,我绑定到_movies,这似乎工作正常。

有人会对为什么会出现这种情况有任何想法吗?

感谢您的帮助。

更新:

根据下面接受的答案,我改变了:

MoviePanelViewModel.cs

 public class MoviePanelViewModel : INotifyPropertyChanged
 {
      private MediaDetail _selectedMovie;
    public MediaDetail SelectedMovie
    {
        get { return _selectedMovie; }
        set
        {
            _selectedMovie = value;
            OnPropertyChanged("SelectedMovie");
        }
    }
 }

1 个答案:

答案 0 :(得分:2)

其中没有一个使用 INotifyPropertyChanged(即提升PropertyChanged事件),因此用户界面无法接收更新。

特别是,MediaDetail甚至不是从INotifyPropertyChanged派生的,所以当你绑定到它的属性时,它无法获取更新:

<!-- Binds to properties of MediaDetail! -->
<Binding Path = "_selectedMovie.title"/>
<Binding Path = "_selectedMovie.year"/>

对于需要将更改传播到UI的任何属性,请确保它引发PropertyChanged并且UI将按预期更新。