Note that there are some explanatory texts on larger screens.

plurals
  1. POPushing read-only GUI properties back into ViewModel
    primarykey
    data
    text
    <p>I want to write a ViewModel that always knows the current state of some read-only dependency properties from the View.</p> <p>Specifically, my GUI contains a FlowDocumentPageViewer, which displays one page at a time from a FlowDocument. FlowDocumentPageViewer exposes two read-only dependency properties called CanGoToPreviousPage and CanGoToNextPage. I want my ViewModel to always know the values of these two View properties.</p> <p>I figured I could do this with a OneWayToSource databinding:</p> <pre><code>&lt;FlowDocumentPageViewer CanGoToNextPage="{Binding NextPageAvailable, Mode=OneWayToSource}" ...&gt; </code></pre> <p>If this was allowed, it would be perfect: whenever the FlowDocumentPageViewer's CanGoToNextPage property changed, the new value would get pushed down into the ViewModel's NextPageAvailable property, which is exactly what I want. </p> <p>Unfortunately, this doesn't compile: I get an error saying <strong>'CanGoToPreviousPage' property is read-only and cannot be set from markup.</strong> Apparently read-only properties don't support <em>any</em> kind of databinding, not even databinding that's read-only with respect to that property.</p> <p>I could make my ViewModel's properties be DependencyProperties, and make a OneWay binding going the other way, but I'm not crazy about the separation-of-concerns violation (ViewModel would need a reference to the View, which MVVM databinding is supposed to avoid).</p> <p>FlowDocumentPageViewer doesn't expose a CanGoToNextPageChanged event, and I don't know of any good way to get change notifications from a DependencyProperty, short of creating another DependencyProperty to bind it to, which seems like overkill here.</p> <p>How can I keep my ViewModel informed of changes to the view's read-only properties?</p>
    singulars
    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.
 

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