简体   繁体   中英

How to refresh a window in C#/WPF?

I want to change a value (textBlock) according to an event. Then, I want to refresh my window, but I couldn't. I used invalidateVisual as well as solutions of other posts, but nothing worked.

Thank you in advance

Several solutions (the first and second one does not make use of databinding).

 txtMyControl.text = "New value";

If not on the main thread, you could use the dispatcher to update the value.

Application.Current.Dispatcher.BeginInvoke(() => txtMyControl.text == "New Value")

However, the most WPF friendly way to do it is to use the databinding. Any change made to the value in code will be instantly reflected in the UI.

XAML

<TextBox x:Name="txtExample"  Text="{Binding MyTextProperty,Mode=TwoWay}" Height="24" Width="120"/>

In your code, you have to declare a variable that will be persistent.

private ExampleModel _ExampleModel = new ExmampleModel();

When you load your code, you associate that variable to your textbox data context.

txtExample.DataContext = _ExampleModel 

Then, you have the class that will contains all the editable properties on screen (textboxes, radio boxes, etc...)

public class ExampleModel : INotifyPropertyChanged
{

private string _MyTextProperty = "test";
public string MyTextProperty {
    get { return _MyTextProperty; }
    set {
        if (string.Compare(_MyTextProperty, value) != 0) {
            _MyTextProperty = value;
            RaisePropertyChanged("MyTextProperty");
        }
    }
}

public void RaisePropertyChanged(string PropertyName)
{
    if (PropertyChanged != null) {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

public event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;
public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
}

Whenever you handle your event, you just have to change the value of the property containing the information and the UI will refresh accordingly. Also, since we use a two-way binding, the value from your textbox will always be the same than the one contained by MyTextProperty property in ExampleModel class, which make value retrieval very easy.
ex:

_ExampleModel.MyTextProperty = "New value";

If you were already using databinding, make sure the class used implements INotifyPropertyChanged and that the propertyChanged event is called when the property value change or otherwise it won't update the UI.

The best approach to what you're trying to do would be to use Data Binding.

You need to have a string object that will always hold the value of your textblock. Next you need to bind that object to your textblock and then use the event provided by the INotifyPropertyChanged interface and each time the value changes its representation (the textblock) will change to, no need to refresh the window.

More information here

If your event updates the textblock and the textblock you are using is bound to a string property and that property issues a NotifyPropertyChanged() in it's set method, that will cause the display to refresh as you desire.

There are other ways, but this is the easiest given my understanding of your question.

(this is similar to the other answer, but I tried to word so it is easier to understand/implement.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM