Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Force a WPF control to refresh?

Force a WPF control to refresh?

Scheduled Pinned Locked Moved WPF
csharpwpfcsswcftutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CooperWu
    wrote on last edited by
    #1

    We have two textBlock 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 textBlock work well on others OS, 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?

    Glad to discuss with you and best wishes.

    N 1 Reply Last reply
    0
    • C CooperWu

      We have two textBlock 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 textBlock work well on others OS, 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?

      Glad to discuss with you and best wishes.

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      It is possible in WPF do an immediate, forced refresh of a control, but it's not as simple as Winform's .Update or .Refresh methods - because the redrawing of the VisualTree doesn't happen while you're GUI thread is busy handling a message from the Dispatcher (which, in your user code, you almost always are). The work around is to temporarily create a Dispatcher "frame", which allows a refresh of the VisualTree to be performed immediately. Apparently this is a bad idea, but it does work. Consider the following helper class:

      public class WpfApplication : Application
      {
      private static DispatcherOperationCallback exitFrameCallback = new
      DispatcherOperationCallback(ExitFrame);

      public static void DoEvents()
      {
      
          DispatcherFrame nestedFrame = new DispatcherFrame();
      
          DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
      
          Dispatcher.PushFrame(nestedFrame);
      
          if (exitOperation.Status != DispatcherOperationStatus.Completed)
          {
              exitOperation.Abort();
          }
      }
      
      private static Object ExitFrame(Object state)
      {
           DispatcherFrame frame = state as DispatcherFrame;
           frame.Continue = false;
           return null;
      }
      

      }

      See also: http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx :) Vote me please

      Niladri Biswas

      L 1 Reply Last reply
      0
      • N Niladri_Biswas

        It is possible in WPF do an immediate, forced refresh of a control, but it's not as simple as Winform's .Update or .Refresh methods - because the redrawing of the VisualTree doesn't happen while you're GUI thread is busy handling a message from the Dispatcher (which, in your user code, you almost always are). The work around is to temporarily create a Dispatcher "frame", which allows a refresh of the VisualTree to be performed immediately. Apparently this is a bad idea, but it does work. Consider the following helper class:

        public class WpfApplication : Application
        {
        private static DispatcherOperationCallback exitFrameCallback = new
        DispatcherOperationCallback(ExitFrame);

        public static void DoEvents()
        {
        
            DispatcherFrame nestedFrame = new DispatcherFrame();
        
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
        
            Dispatcher.PushFrame(nestedFrame);
        
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
        
        private static Object ExitFrame(Object state)
        {
             DispatcherFrame frame = state as DispatcherFrame;
             frame.Continue = false;
             return null;
        }
        

        }

        See also: http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx :) Vote me please

        Niladri Biswas

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Tried this approach when doing TabControl.Items.MoveCurrentTo... and it has no effect. The tab control is still not redrawn. Any ideas? Thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups