locked
Snapped View Style Not Working

    Question

  • I have a TextBlock that looks like this:

    <common:LayoutAwarePage.Resources>
            <CollectionViewSource x:Name="CollectionViewSource" x:Key="CollectionViewSource" />
            <Style x:Key="SnappedStyle" TargetType="TextBlock" BasedOn="{StaticResource MyStyle}">
                <Setter Property="Margin" Value="0,39,10,0"/>
            </Style>
    ...
    
    <TextBlock x:Name="MyTextBlock" x:Uid="SomeValue" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="99,39,10,0" Style="{StaticResource MyStyle}"/>
    ...
    
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="ApplicationViewStates">
        <VisualState x:Name="Snapped">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Style)" Storyboard.TargetName="MyTextBlock">
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedStyle}"/>
            </ObjectAnimationUsingKeyFrames>
    ...


    When I go into Snapped View, I'd like the 99px left margin to disappear, so I give my TextBlock a new style that removes that margin. However, the Margin value doesn't have any effect, and I'm left with the original 99px left margin. Any idea why this is happening?


    • Edited by inspira Tuesday, July 3, 2012 9:16 PM
    Tuesday, July 3, 2012 9:14 PM

Answers

  • You specify a margin value in your control (Margin="99,39,10,0").

    When the style is applied, the control's margin property is not modified as you expect because the control already has a margin property. The Style only modifies properties that are not set in the control.

    You should define two styles for your control. One style for standard view with margin "99,39,10,0", and another style for snapped view with margin "0.39.10.0". And don't set the Margin property in your control.

    • Marked as answer by inspira Thursday, July 12, 2012 6:40 PM
    Wednesday, July 11, 2012 12:50 AM

All replies

  • You should check if the weight is out of border. 

    NEU_ShieldEdge

    Thursday, July 5, 2012 4:53 AM
  • I don't understand, there is no "weight" property, nor am I using a Border object.

    If you mean width, I don't see how that would affect my problem. I'm unable to set a specific property for a XAML object. I can set the foreground, fontsize or pretty much any other property I want using the method I outlined above, except for the margin.

    Thursday, July 5, 2012 7:00 PM
  • You specify a margin value in your control (Margin="99,39,10,0").

    When the style is applied, the control's margin property is not modified as you expect because the control already has a margin property. The Style only modifies properties that are not set in the control.

    You should define two styles for your control. One style for standard view with margin "99,39,10,0", and another style for snapped view with margin "0.39.10.0". And don't set the Margin property in your control.

    • Marked as answer by inspira Thursday, July 12, 2012 6:40 PM
    Wednesday, July 11, 2012 12:50 AM
  • Thanks for the clarification, I didn't realize styles only modify properties that are not set in the control. I started out setting the Margin and other properties one by one in the VisualStateManager, which led me to assume if I bundled everything in a style, it would work the same way and I could save some lines of code.
    Thursday, July 12, 2012 6:40 PM