2009-05-04 250 views
12

我们有两个的TextBlocks这样的:(我们使用.NET FW 3.0)强制WPF控件刷新?

<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> 

<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> 

这两个的TextBlocks其它OS-ES工作得很好,但有时错过在Windows XP带有SP3的家庭版本。我们尝试了很多方法来刷新这些,但失败了。

我们尝试:

  1. UpdateLayout请
  2. InvalidateVisual
  3. 改变了一套Text属性在代码中结合模式。

如何强制这些控件刷新?

+1

(x作为的UIElement).InvalidateMeasure() – 0x4f3759df 2011-11-08 17:55:45

回答

4

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(); 

它运作良好。

+0

Whats`NoArgsHandle`我需要为它创建一个类吗? – raym0nd 2011-08-05 16:00:40

4

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

退房Rudi Grobler10件事情我不知道WPF数据绑定

一些数据绑定的文章:

  1. WPF Data Binding - Part 1通过乔尔象牙约翰逊alt text
  2. Moving Toward WPF Data Binding One Step at a Time由约什 - 史密斯
+0

我不认为这可以解决这个问题,因为我们已经尝试单向绑定。 – 2009-05-04 13:57:32

16

这对我们而言无需创建新线程。它计划在所有绑定首先更新自己时开始的操作。

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