Reputation:
I have the following code:
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));
}
The problem I am having is that my UI
is not being updated. That is _selectedMovie.title
, _selectedMovie.year
and _selectedMovie.rating
are not being updated.
In another part of my XAML
, I bind to _movies
and this seems to work fine.
Would anyone have any ideas as to why this might be the case?
Thank you for your help.
UPDATE:
Based on the accepted answer below I changed:
MoviePanelViewModel.cs
public class MoviePanelViewModel : INotifyPropertyChanged
{
private MediaDetail _selectedMovie;
public MediaDetail SelectedMovie
{
get { return _selectedMovie; }
set
{
_selectedMovie = value;
OnPropertyChanged("SelectedMovie");
}
}
}
Upvotes: 0
Views: 485
Reputation: 61369
None of that actually is using INotifyPropertyChanged
(ie, raising the PropertyChanged
event), so the UI can't receive the updates.
In particular, MediaDetail
doesn't even derive from INotifyPropertyChanged
, so when you bind to its properties, there is no way it will pick up the updates:
<!-- Binds to properties of MediaDetail! -->
<Binding Path = "_selectedMovie.title"/>
<Binding Path = "_selectedMovie.year"/>
For any property whose changes need to propagate to the UI, make sure it raises PropertyChanged
and the UI will update as expected.
Upvotes: 2