I have some Button in my MainPage as shown below:
<Grid Grid.Row="1">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" >
<Image Stretch="None" Source="Images/GameMainMenuIcon.png" Margin="0 0 0 50" />
<Button Style="{StaticResource ButtonGameLarge}">Button1</Button>
<Button Style="{StaticResource ButtonGameLarge}">Button2</Button>
<Button Style="{StaticResource ButtonGameLarge}">Button3</Button>
</StackPanel>
</Grid>
And in my Style.xaml file I wrote these styles for the Buttons:
<Style TargetType="Button" x:Key="ButtonGameLarge">
<Setter Property="VisualStateManager.VisualStateGroups">
<Setter.Value>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Height" Value="80" />
<Setter Property="Width" Value="400" />
<Setter Property="Margin" Value="0 0 0 20" />
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="400" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Height" Value="40" />
<Setter Property="Width" Value="200" />
<Setter Property="Margin" Value="0 0 0 10" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</Setter.Value>
</Setter>
</Style>
But it gives me these errors:
- The property "VisualStateGroups" does not have an accessible setter.
- Cannot assign 'VisualStateGroup' into property 'VisualStateGroups', type must be assignable to 'IList'
- The property "VisualStateGroups" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "VisualStateGroups". For attached properties, the declaring type must provide static "GetVisualStateGroups" and "SetVisualStateGroups" methods.
How can I set multiple VisualState for a group of elements?
The attached property
VisualStateManager.VisualStateGroupscan be only applied to types that are derived fromFrameworkElement.For your problem, you can modify the style like this:
As it in my code, we can attach your
VisualStateManager.VisualStateGroupsto theRootGridinside ofButtonwhich derived fromFrameworkElement.