1

I am trying to switch from using triggers and themes to VisualStateManager because it appears that WindowsRT is moving in this direction and I want to minimize the amount of code that is different. To that effect, I'm trying to set a simple scheme that will switch between large margins for a tablet device, such as the Surface 2, and normal looking margins for a desktop scheme. I know I can do this by setting the property directly on the object, but if I have 15 labels in a control, it's simply unsupportable to create a storyboard that sets the margin for every label. So I'm attempting to swap the styles on each of the labels with this code:

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="DeviceStates">
                <VisualState Name="Desktop"/>
                <VisualState Name="TabletLandscape">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FirstNameLabel"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource TabletLabelStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

I've seen several examples in my search that show an almost identical pattern working in Silverlight and in Windows Store applications. When I try this in plain-old WPF, I get "This freezable cannot be frozen". I dug into the code with Reflector and Style is not derived from DependencyObject, so I'm confused about what's trying to be frozen. Does anyone have a clue what's going on here?

Quark Soup
  • 4,272
  • 3
  • 42
  • 74

1 Answers1

1

I had a similar problem and found a few other questions like this with either no answer or no good answers. My Style only changed basic properties, like font size and color, without any "freezables" in site. It's a strange bug in WPF, IMHO.

After some trial and error, I seem to have gotten it working by simply moving my resource definitions (to which the StaticResource points) from the user control in which they were being used to an external resource dictionary, which I then included in App.xaml. Furthermore, the user control causing this problem is defined in a custom control project, so basically I removed the Style resources from the control project and placed them into my .exe project, which the control has no problem resolving at runtime. (Note that designer errors may be safely ignored.)

I'm not sure whether it was moving the resources out of the user control or out of the project, or both, that solved the problem for me.

Dave Sexton
  • 2,562
  • 1
  • 17
  • 26