I would like to ask if there is something like ObservableCollection but for object in WPF C#.
Here is my problem:
I have following Grid in XAML:
<Grid x:Name="detailsGrid">
<TextBox Text="{Binding Name, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" />
<TextBox Text="{Binding Excerpt, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" />
</Grid>
In my code behind I set the DataContext of detailsGrid to object of following class:
recipeDetails = new RecipeDetailsContainer();
this.detailsGrid.DataContext = recipeDetails;
RecipeDetailsContainer class:
public class RecipeDetailsContainer : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
private string _excerpt;
public string Excerpt
{
get { return _excerpt; }
set
{
_excerpt = value;
NotifyPropertyChanged("Excerpt");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
In other class method I want to write this info to WPF window, so I set:
private void PrintInfoAboutRecipe(object sender, RecipeHandlerArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
if (e.Container != null && e.Container.Recipe != null)
{
this.recipeDetails.Name = e.Container.Recipe.Name;
this.recipeDetails.Excerpt = e.Container.Recipe.Excerpt;
}
else
{
this.recipeDetails.Name = "";
this.recipeDetails.Excerpt = "";
}
}));
}
But my binding does not work and nothing is showed in TextBlocks.
Am I doing something wrong? Any help would be appreciated. Thanks! :-)