Question

We have two textBlocks like this: (we used .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.

We tried:

  1. UpdateLayout
  2. InvalidateVisual
  3. Changed the set Text property in code to binding mode.

How to force these controls to refresh?

Was it helpful?

Solution 2


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.

OTHER TIPS

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.
                    }));

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.

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

Some databinding articles:

  1. WPF Data Binding - Part 1 By Joel Ivory Johnson alt text
  2. Moving Toward WPF Data Binding One Step at a Time By Josh Smith
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top