Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF binding not updating the view

Tags:

c#

mvvm

wpf

xaml

I have a textblock:

<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText}">
            ... Status ...
</TextBlock>

codebehind:

public StatusPage()
{
    InitializeComponent();
    this.DataContext = new StatusPageViewModel(this);
}

and in viewModel:

private string _statusText;
/// <summary>
/// Status text
/// </summary>
public string StatusText
{
    get { return _statusText; }
    set { _statusText = value; }
}

and in function in viewModel:

string statusText = Status.GetStatusText();
this.StatusText = statusText;

GetStatusText() returns string like "Work done" etc. Values from that functions are assinged to the this.StatusText but the TextBlock's text property don't change and is showing still placeholder "... Status..."

I'm aware of questions like this --> CLICK<--- but after reading this I'm still not able to find solution

@Update

After your suggestions i updated my code and now I have this:

public string StatusText
{
    get 
    {
        return _statusText;
    }
    set 
    {
        _statusText = value; 
        RaisePropertyChanged("StatusText");
    }
}

and declaration of viewModel:

 public class StatusPageViewModel : ObservableObject, INavigable

where:

ObservableObject class is:

public abstract class ObservableObject : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    /// <summary>
    /// Raises the PropertyChange event for the property specified
    /// </summary>
    /// <param name="propertyName">Property name to update. Is case-sensitive.</param>
    public virtual void RaisePropertyChanged(string propertyName)
    {
        OnPropertyChanged(propertyName);
    }

    /// <summary>
    /// Raised when a property on this object has a new value.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    #endregion // INotifyPropertyChanged Members
}

But its still not working

like image 266
szpic Avatar asked Aug 27 '14 08:08

szpic


People also ask

What is INotifyPropertyChanged in WPF?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.

How do you use INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is Updateourcetrigger WPF?

The UpdateSourceTrigger property of a binding controls how and when a changed value is sent back to the source. However, since WPF is pretty good at controlling this for you, the default value should suffice for most cases, where you will get the best mix of a constantly updated UI and good performance.


3 Answers

You need to implement INotifyPropertyChanged in your ViewModel order to notify the View that the property has changed.

Here's a link to the MSDN page for it: System.ComponentModel.INotifyPropertyChanged

The most important thing to note is that you should raise the PropertyChanged event in your property setter.

like image 79
toadflakz Avatar answered Nov 15 '22 06:11

toadflakz


Add binding mode two way, because by default Textblock's binding mode is one way

<TextBlock HorizontalAlignment="Left" Name="StatusText" Margin="0,20" TextWrapping="Wrap" Text="{Binding StatusText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            ... Status ...
</TextBlock>

and also, of course you need to implement INotifyPropertyChanged for the purpose, refer to this link for how to implement.

like image 35
Mayur Dhingra Avatar answered Nov 15 '22 05:11

Mayur Dhingra


When working with DataModels you have to be sure the model is complete at intial load. So if you do this: this.DataContext = mainViewModel and some parts of you mainViewModel are NOT loaded (=null) then you are not able to bind them. Example, I have a Model within that model an object Program. I bind to Text of a TextBlock to Model.Program.Name. The Program object is not connected at initial load so you will have to rebind to a loaded object after because otherwise no notifications can be send.

like image 6
Herman Van Der Blom Avatar answered Nov 15 '22 04:11

Herman Van Der Blom