Note that there are some explanatory texts on larger screens.

plurals
  1. POVariables in XAML Style
    text
    copied!<p>I have the following <code>Style</code>:</p> <pre><code>&lt;Style x:Key="ButtonBase" TargetType="Button"&gt; &lt;Setter Property="Background" Value="#FF007BFF"/&gt; &lt;Setter Property="Foreground" Value="White"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="Button"&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsPressed" Value="&gt; &lt;Setter Property="Background" Value="Yellow" /&gt; &lt;Setter Property="Foreground" Value="Black" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsPressed" Value="False"&gt; &lt;Setter Property="Background" Value="#FF007BFF" /&gt; &lt;Setter Property="Foreground" Value="White" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>and an inherited <code>Style</code>:</p> <pre><code>&lt;Style x:Key="ElementButton" TargetType="Button" BasedOn="{StaticResource ButtonBase}"&gt; &lt;Setter Property="Margin" Value="10"/&gt; &lt;Setter Property="Height" Value="200"/&gt; &lt;/Style&gt; </code></pre> <p>What I'd like to do is be able to set an arbitrary variable in the base style:</p> <pre><code>&lt;Setter Variable="HoverColor" Value="Pink"/&gt; </code></pre> <p>Then I'd be able to use my triggers as such:</p> <pre><code>&lt;Trigger Property="IsPressed" Value="&gt; &lt;Setter Property="Background" Value="{TemplateBinding HoverColor}" /&gt; &lt;Setter Property="Foreground" Value="Black" /&gt; &lt;/Trigger&gt; </code></pre> <p>And finally, I could then override it in my inherited style:</p> <pre><code>&lt;Style x:Key="ElementButton" TargetType="Button" BasedOn="{StaticResource ButtonBase}"&gt; &lt;Setter Property="Margin" Value="10"/&gt; &lt;Setter Property="Height" Value="200"/&gt; &lt;Setter Variable="HoverColor" Value="Red"/&gt; &lt;/Style&gt; </code></pre> <p>Is there a way to achieve this? I've already looked at static resources but these can't be overridden. Also, I cannot use anything that requires a code-behind because I don't have one!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload