Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding MVVM: simple non-working code
    primarykey
    data
    text
    <p>I'm trying to understand this pattern and all the logic that's behind it.</p> <p>I don't think it's that hard, but still I'm failing in some easy tasks.</p> <p>Let's make it clear with a non-working example that I wrote:</p> <p>Model:</p> <pre><code>public class Model { public string Name { get; set; } public string Description { get; set; } public Categories Category { get; set; } public Grid PresenterContent { get; set; } } </code></pre> <p>ViewModel:</p> <pre><code>public class ViewModel : ViewModelBase { private Model _model; public Model Model { get { return _model; } set { if (_model != value) { _model = value; RaisePropertyChanged(() =&gt; Model); } } } public Grid PresenterContent { get { return Model.PresenterContent; } private set { } } public ViewModel() { Model = new Model(); } } </code></pre> <p>View:</p> <pre><code>&lt;UserControl.DataContext&gt; &lt;Binding Source="ViewModel"/&gt; &lt;/UserControl.DataContext&gt; &lt;Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"&gt; &lt;ContentPresenter Content="{Binding PresenterContent}"/&gt; &lt;/Grid&gt; </code></pre> <p>Now, I expect this to work when I run it, as I'm setting the <code>DataContext</code> to <code>ViewModel</code> which has a <code>PresenterContent</code> property.</p> <p>(This property is both in the <code>Model</code> and the <code>ViewModel</code> because I don't know to work with child's property, in this case <code>Model.PresenterContent</code>.)</p> <p>What actually happens is that an exception is thrown:</p> <blockquote> <p>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')..</p> </blockquote> <p>and this says that there's no <code>PresenterContent</code> in the <code>ViewModel</code>, which is clearly wrong. The exception is the same if I try to bind to <code>Model</code> property.</p> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload