none
Force style on control to update RRS feed

  • Question

  • I have a custom control on which the style is not being updated (visually). When I step through the code in the debugger, and look at the style, it reflects the correct style but it is not reflected visually.  How can I force the control to update it's style? 

    The control is redrawing, it is just redrawing with the "old" style.

    Thursday, April 23, 2009 2:21 PM

Answers

  • The problem was completely my fault.  I was drawing geometry manually, and it was getting the correct styles when initialized, but it was never updating to the newly set style on each repaint.
    • Marked as answer by Frankenspank Monday, May 4, 2009 1:25 PM
    Monday, May 4, 2009 1:25 PM

All replies

  • Hi,

    what exactly did you do? Set the control's Style property to a new style? Or changed the properties of the style that was already present in the control's Style property?
    Thursday, April 23, 2009 2:44 PM
  • In a custom control you can call InvalidateVisual method (or InvalidateMeasure / InvalidateArrange if that's the only thing you need).

    Cheers,
    -Sergi
    Thursday, April 23, 2009 3:23 PM
  • Are you using StaticResource or DynamicResource to point to a keyed style?  If you use an unkeyed Style or a DynamicResource Style it should update if you inject a Style into the Resource Lookup Tree before your default one.  I believe StaticResource will only allow it to be set once.
    If this was helpful, please mark as answered
    Blog: AttachedWPF
    AttachedWPF
    Friday, April 24, 2009 12:37 PM
  • OK, I tried the InvalidateVisual which did not work. What I have is a progress bar control. The user supplies a list of stops that will change the color of the bar element when the bar gets to a certain point.  this logic is working fine, and as I mentioned the style when I step through in the debugger is the correct one.

    This is what I do to set the style. newStyle is set by calling "FindResource", which I believes make it a dynamic resource.

     this.IndicatorStyle = newStyle;

    ...

    Here is the important info out of my template for the control:

        <Canvas Grid.Row="1" Grid.RowSpan="3">
          <c:CircularPercentageIndicator VerticalAlignment="Center" HorizontalAlignment="Center" Width="150" Height="150"  Percent="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type c:CircularProgressBar}},Path=Value}"
                                                     Style="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type c:CircularProgressBar}},Path=IndicatorStyle}"
                          x:Name="illustration" >

          </c:CircularPercentageIndicator>
        </Canvas>

    ...

      <Style
         TargetType="{x:Type bevelcontrols:CircularProgressBar}">
          <Setter Property="Template"  Value="{StaticResource CircularProgressBarTemplate}" />
        <Setter Property="IndicatorStyle" Value="{DynamicResource StandardDonut}" />
        </Style>

       
    Friday, April 24, 2009 6:34 PM
  • I see what you're getting at.  What is interesting is that if I set the value of the progress bar and everything is initialized on form load, the new style is reflected, but after that the new style does not get reflected. How would I load an unkeyed style at runtime anyway?
    Friday, April 24, 2009 6:40 PM
  • Is IndicatorStyle a dependency property or else are you implementing INotifyProperty changed to signal that IndicatorStyle has changed?
    Friday, April 24, 2009 6:44 PM
  • It might be easier for you to create a Style and within that style have triggers. 

    I.E:

        <Style TargetType="{x:Type c:CircularPercentageIndicator}">
            <Style.Triggers>
                <Trigger Property="Percent" Value="15">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="Percent" Value="50">
                    <Setter Property="Background" Value="Yellow"/>
                </Trigger>
                <Trigger Property="Percent" Value="100">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    Then remove the style tag in your control and it should work.
    Friday, April 24, 2009 6:45 PM
  • Triggers have never really worked the way I wanted.  What I really want to do is provide an expression, then it would work.

    This code wouldn't work for me since the progress indicator isn't always going to be jumping by 1 and can be any double value between 0-1.  Also, the style would be set back to whatever it was initialized at.
    Friday, April 24, 2009 8:16 PM
  • The problem was completely my fault.  I was drawing geometry manually, and it was getting the correct styles when initialized, but it was never updating to the newly set style on each repaint.
    • Marked as answer by Frankenspank Monday, May 4, 2009 1:25 PM
    Monday, May 4, 2009 1:25 PM