Note that there are some explanatory texts on larger screens.

plurals
  1. PORefreshing Read-Only (Chained) Property in MVVM
    text
    copied!<p>I'm thinking this should be easy, but I can't seem to figure this out.</p> <p>Take these properties from an example ViewModel (ObservableViewModel implements INotifyPropertyChanged):</p> <pre><code>class NameViewModel : ObservableViewModel { Boolean mShowFullName = false; string mFirstName = "Wonko"; string mLastName = "DeSane"; private readonly DelegateCommand mToggleName; public NameViewModel() { mToggleName = new DelegateCommand(() =&gt; ShowFullName = !mShowFullName); } public ICommand ToggleNameCommand { get { return mToggleName; } } public Boolean ShowFullName { get { return mShowFullName; } set { SetPropertyValue("ShowFullName", ref mShowFullName, value); } } public string Name { get { return (mShowFullName ? this.FullName : this.Initials); } } public string FullName { get { return mFirstName + " " + mLastName; } } public string Initials { get { return mFirstName.Substring(0, 1) + "." + mLastName.Substring(0, 1) + "."; } } } </code></pre> <p>The guts of such a [insert your adjective here] View using this ViewModel might look like:</p> <pre><code>&lt;TextBlock x:Name="txtName" Grid.Row="0" Text="{Binding Name}" /&gt; &lt;Button x:Name="btnToggleName" Command="{Binding ToggleNameCommand}" Content="Toggle Name" Grid.Row="1" /&gt; </code></pre> <p>The problem I am seeing is when the ToggleNameCommand is fired. The ShowFullName property is properly updated by the command, but the Name binding is never updated in the View. </p> <p>What am I missing? How can I force the binding to update? Do I need to implement the Name properties as DependencyProperties (and therefore derive from DependencyObject)? Seems a little heavyweight to me, and I'm hoping for a simpler solution.</p> <p>Thanks, wTs</p>
 

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