Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell UI that a bound WPF property, changed within the model, has updated
    primarykey
    data
    text
    <p>I'm new to WPF, trying to adhere to the MVVM pattern as best as I can. So far so good except I've run into a problem with binding certain properties from my model.</p> <p>So I have non-static properties that I have exposed in my model but they can only be changed from within the model. I run some function that does a bunch of stuff and it keeps track of what it's doing through a bunch of parameters that I've exposed for viewing.</p> <p>I'm ok when I've got properties in my ViewModel - I can update these ok because I have implemented INotifyPropertyChanged. I've seen that sometimes people have implemented this in their Model too so I tried that but not knowing how INotifyPropertyChanged really works I don't know if there's anything else I need to do to get it running ok once it's in the Model.</p> <p>I tried to create a property in my ViewModel that read from the Model and I bound the xaml to this but because I can't alter it from the ViewModel I ran into the same problem of telling the UI that it has been changed. Currently I have the binding direct whilst I try and figure this out but my goal is to be able to bind to a property in the ViewModel that just grabs the value from the Model.</p> <p>Can anyone give me a good simple example of one-way binding to basic controls like labels/textblocks etc that will update itself when it all changes from within the model? </p> <p>For completeness here is a simplified version of what I have including sample xaml (showing binding to a Model property &amp; binding to a property from the ViewModel). The binding works because if I make changes in the model they appear in the designer and initial build.</p> <p>The model is my own code and I can add/remove anything to get it working. Maybe it's fairly straightforward but I'm just not seeing the solution at the moment and not seen anything that makes sense to me on the forums.</p> <p>Thanks!</p> <h3>in the Model</h3> <pre><code>public enum TempValues { zero, pos10, pos50, pos100 } namespace AutoCalModel { public class AutoCalibration : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private TempValues _TempRelayValue = TempValues.zero; public TempValues TempRelayValue { get { return _TempRelayValue; } set { if (!value.Equals( _TempRelayValue)) { _TempRelayValue = value; NotifyPropertyChanged("TempRelayValue"); } } } // rest of class including code that changes the above TempRelayValue // accessed through the public property only } } </code></pre> <h3>in the xaml</h3> <pre><code>&lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center"&gt; &lt;TextBlock Name="labelPrsTitle" Text="Prs:" Margin="2,0,2,0"/&gt; &lt;TextBlock Name="labelPrsValue" Text="{Binding Path=currentPrsValueString, Mode=OneWay}" Margin="2,0,5,0"/&gt; &lt;Separator Margin="5,0,5,0"/&gt; &lt;TextBlock Text="Temp Relay:" Margin="5,0,2,0"/&gt; &lt;TextBlock Text="{Binding Path=TempRelayValue, Converter={StaticResource tempValuesConverter}, Mode=OneWay}" Margin="2,0,5,0"&gt; &lt;TextBlock.DataContext&gt;&lt;Model:AutoCalibration/&gt;&lt;/TextBlock.DataContext&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; </code></pre>
    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.
    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