I'm trying to understand this pattern and all the logic that's behind it.
I don't think it's that hard, but still I'm failing in some easy tasks.
Let's make it clear with a non-working example that I wrote:
Model:
public class Model
{
public string Name { get; set; }
public string Description { get; set; }
public Categories Category { get; set; }
public Grid PresenterContent { get; set; }
}
ViewModel:
public class ViewModel : ViewModelBase
{
private Model _model;
public Model Model
{
get
{
return _model;
}
set
{
if (_model != value)
{
_model = value;
RaisePropertyChanged(() => Model);
}
}
}
public Grid PresenterContent
{
get
{
return Model.PresenterContent;
}
private set { }
}
public ViewModel()
{
Model = new Model();
}
}
View:
<UserControl.DataContext>
<Binding Source="ViewModel"/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<ContentPresenter Content="{Binding PresenterContent}"/>
</Grid>
Now, I expect this to work when I run it, as I'm setting the DataContext
to ViewModel
which has a PresenterContent
property.
(This property is both in the Model
and the ViewModel
because I don't know to work with child's property, in this case Model.PresenterContent
.)
What actually happens is that an exception is thrown:
System.Windows.Data Error: BindingExpression path error: 'PresenterContent' property not found on 'ViewModel' 'System.String' (HashCode=-903444198). BindingExpression: Path='PresenterContent' DataItem='ViewModel' (HashCode=-903444198); target element is 'System.Windows.Controls.ContentPresenter' (Name=''); target property is 'Content' (type 'System.Object')..
and this says that there's no PresenterContent
in the ViewModel
, which is clearly wrong.
The exception is the same if I try to bind to Model
property.
What am I doing wrong?