强制刷新 WPF 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/818911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:21:47  来源:igfitidea点击:

Force a WPF control to refresh?

wpfcontrolsrefresh

提问by Cooper.Wu

We have two textBlocks like this: (we used .NET FW 3.0)

我们有两个这样的文本块:(我们使用 .NET FW 3.0)

<TextBlock Grid.Column="0" Name="tabName" Style="{StaticResource textBlockBarStyle}" HorizontalAlignment="Left">
  <TextBlock.Margin>
      <Binding Converter="{StaticResource dpiConverter}">
          <Binding.ConverterParameter>
               <Thickness Left="3" Top="6" Right="0" Bottom="0"/>
          </Binding.ConverterParameter>
      </Binding>
  </TextBlock.Margin>
</TextBlock>

and

<TextBox  x:Name="txtBoxHelp" 
          IsReadOnly="True" Style="{DynamicResource txtBoxHelpStyle}" 
          IsTabStop="False" 
          Text="some text" MouseLeftButtonDown="txtBoxHelp_MouseLeftButtonDown">
     <TextBox.Margin>
        <Binding Converter="{StaticResource dpiConverter}">
            <Binding.ConverterParameter>
                 <Thickness Left="7" Top="0" Right="0" Bottom="0"/>
            </Binding.ConverterParameter>
        </Binding>
     </TextBox.Margin>
</TextBox>

These two textBlocks work well on other OS-es, but sometimes miss on the Windows XP Home Version with SP3. We have tried many ways to refresh these, but failed.

这两个文本块在其他操作系统上运行良好,但有时会在带有 SP3 的 Windows XP 家庭版上失败。我们尝试了很多方法来刷新这些,但都失败了。

We tried:

我们尝试了:

  1. UpdateLayout
  2. InvalidateVisual
  3. Changed the set Text property in code to binding mode.
  1. 更新布局
  2. 无效视觉
  3. 将代码中设置的 Text 属性更改为绑定模式。

How to force these controls to refresh?

如何强制这些控件刷新?

采纳答案by Cooper.Wu


Thread thread = new Thread(new ThreadStart(delegate()
                {
                    Thread.Sleep(200); // this is important ...
                    try
                    {
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Send,
                            new NoArgsHandle(delegate()
                            {
                               // do something, set .Text = "some text"
                            }));
                    }
                    catch { }
                }));
                thread.Name = "thread-UpdateText";
                thread.Start();

It works well.

它运作良好。

回答by Helper

This works for us without the need to create a new thread. It schedules the action to start when all of the bindings have updated themselves first.

这对我们有用,无需创建新线程。它安排在所有绑定首先更新自己时开始的操作。

           Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                new Action(() =>
                    {
                        // Do something here.
                    }));

回答by Soni Ali

The way to make controls live update in WPF is by TwoWay databinding. So make sure all the viewModel properties you bind to are Dependency Properties or implement INotifyPropertyChanged (and handled correctly) and also that their Binding.Mode = TwoWay.

在 WPF 中进行控件实时更新的方法是通过 TwoWay 数据绑定。因此,请确保您绑定到的所有 viewModel 属性都是依赖属性或实现 INotifyPropertyChanged(并正确处理)以及它们的 Binding.Mode = TwoWay。

Check out Rudi Grobler's 10 things I didn't know about WPF data binding

查看Rudi Grobler关于 WPF 数据绑定我不知道10 件事

Some databinding articles:

一些数据绑定文章:

  1. WPF Data Binding - Part 1By Joel Ivory Johnson alt text
  2. Moving Toward WPF Data Binding One Step at a TimeBy Josh Smith
  1. WPF 数据绑定 - 第 1 部分作者:Joel Ivory Johnson替代文字
  2. 一步一步地转向 WPF 数据绑定作者:Josh Smith