如何在 C#/WPF 中刷新窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26871641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh a window in C#/WPF?
提问by Vasileios
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.
我想根据事件更改值 (textBlock)。然后,我想刷新我的窗口,但我不能。我使用了 invalidateVisual 以及其他帖子的解决方案,但没有任何效果。
Thank you in advance
先感谢您
回答by Sage Pourpre
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.
但是,最 WPF 友好的方法是使用数据绑定。对代码中的值所做的任何更改都会立即反映在 UI 中。
XAML
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:
每当您处理事件时,您只需更改包含信息的属性的值,UI 就会相应地刷新。此外,由于我们使用双向绑定,文本框中的值将始终与 ExampleModel 类中的 MyTextProperty 属性包含的值相同,这使得值检索非常容易。
前任:
_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.
如果您已经在使用数据绑定,请确保使用的类实现 INotifyPropertyChanged 并且在属性值更改时调用 propertyChanged 事件,否则不会更新 UI。
回答by dimlucas
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 INotifyPropertyChangedinterface and each time the value changes its representation (the textblock) will change to, no need to refresh the window.
您需要有一个字符串对象,该对象将始终保存您的文本块的值。接下来,您需要将该对象绑定到您的文本块,然后使用INotifyPropertyChanged接口提供的事件,每次值更改其表示(文本块)时都会更改为,无需刷新窗口。
More information here
更多信息在这里
回答by JustMeToo
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.
如果您的事件更新了文本块并且您正在使用的文本块绑定到一个字符串属性,并且该属性在它的 set 方法中发出 NotifyPropertyChanged(),这将导致显示根据您的需要刷新。
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.)
(这与另一个答案类似,但我试着用语言表达,这样更容易理解/实施。)

