Search code examples
xamluwpwin-universal-appuwp-xaml

Property attribute of Setter in VisualState.Setters generates syntax error


I want to make a custom style for my UWP button. So I copied the button style template from MSDN and then only modified the "PointerOver" Visual State to make sure I got the gist of it.

Below is a snippet what I have so far. It is in a separate file called CustomButtonStyleTemplate.xaml and in my main xaml file I imported it using <ResourceDictionary.MergedDictionaries/>.

<Style TargetType="Button" x:Key="SpecialButton">
    ...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="ButtonGrid" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="ButtonGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver">
                                <VisualState.Setters>
                                    <Setter Property="ButtonGrid.Opacity" Value="0.5"/>
                                </VisualState.Setters>
                            </VisualState>
                            .
                            .
                            .

Unfortunately, My custom <Setter/> is generating the following error: The XAML Binary Format (XBF) generator reported syntax error.

Whenever I look for similar code online, most people set the Property field of the <Setter/> to something like MyButton.Opacity, where some <Button/> element has x:Name="MyButton".

The thing is, I want this style to apply to many buttons that I will explicitly choose, hence why I specified x:Key="SpecialButton" for the <Style/>.

I thought since the <Grid/> has x:Name="ButtonGrid", I should use ButtonGrid.Opacity but clearly I am mistaken. I should note that ButtonGrid.(UIElement.Opacity) didn't work either.

How then should I refer generically to the Opacity property of which ever button this style applies to?


Solution

  • Replace Property="ButtonGrid.Opacity" to Target="ButtonGrid.Opacity" .

    Like this:

    <Setter Target="ButtonGrid.Opacity" Value="0.5"/>