This is my code
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="Background" Value="{StaticResource LightButtonBackground}"/>
<Setter Property="Foreground" Value="White" />
</VisualState.Setters>
</VisualState>
The compiler says XamlCompiler error WMC0615: Type 'StaticResource' used after '{' must be a Markup Extension. Error code 0x09ff.
XamlCompiler error WMC0615: Type 'StaticResource' used after '{' must be a Markup Extension. Error code 0x09ff.
I didn't find any useful information on the network. What's wrong?
You're mixing the two usages of the Setter here.
The Property
property can be used only when defining a Style
:
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
In a style definition the targeted control type is already known (It is provided by Style.TargetType
).
In a VisualState.Setters
list, you are not defining a Style
. You are altering some properties on some existing child controls . In this case, you need to use the Target
property to let the XAML runtime know which element and properties you are targeting.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="NarrowState">
<VisualState.Setters>
<Setter Target="myPanel.Orientation" Value="Vertical"/>
<Setter Target="myPanel.Width" Value="380"/>
<Setter Target="myTextBlock.MaxLines" Value="3"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel x:Name="myPanel" Orientation="Horizontal">
<TextBlock x:Name="myTextBlock" MaxLines="5" Style="{ThemeResource BodyTextBlockStyle}"/>
</StackPanel>
</Grid>
As @Vincent answer points out, you are not utilizing the setter correctly when defining it inside a visual state, but instead utilizing as you would when defining a style resource. Nevertheless my answer is an insight on how you can utilize a defined StaticResource
on a style, analyzing the two situations and whether it is possible or not.
With that said, I don't think it's possible to define a property of a style by setting it onto a resource.
What you are actually looking for is the BasedOn
, property which allows to inherit styles. The only downside is that Styles
that inherit from other styles must target the same type of control or either a control that derives from the type targeted by the base style.
Take a look onto the documentation, here: https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/xaml-styles#use-based-on-styles
Edit:
Unfortunately, I don't think it's possible to define your VisualState
style to inherit from a resource using the BasedOn
, since we are forced to specify every setter on its definition. Might we be under an XY problem ? On a resource definition, that would be the way to go if you want to inherit from a style, but actually applying it on a visual state seems to be another completely story.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.