Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Its a good question and I've fought through this sort of thing as well. There may be some kind of XAML-only approach that could work, but I have a feeling that if there is, it would feel pretty kludgy. I have a couple of suggestions of how achieve what you want.</p> <p>First, a quick observation. You say you "don't have a code-behind" and that your view is "XAML-only". Well, I've never seen a UserControl View that doesn't have <em>any</em> code-behind <em>file</em> at least, so I'm assuming you mean you just don't want to put any code in there (other than the obligatory <code>InitializeComponent()</code>). Having said that, the approaches that I'll outline won't require code in your code-behind files.</p> <p>At the end of the day, it sounds like what you really want is define some custom "variables". These suggestions do just that, albeit maybe not in the way that you originally envisioned doing so.</p> <p>The <strong>first approach</strong> that would solve your problem is to <strong>subclass the control</strong> that you are interested in styling and add any custom dependency properties to it. For example, you could subclass <code>Button</code>, to say something like <code>ButtonWithMyVariables</code>. One of those custom dependency properties would be called "HoverColor", of type <code>Color</code>, or perhaps more appropriately, "HoverBrush" of type <code>Brush</code> (if you're wanting to just apply it directly to the background or foreground property). Then your base Style can set HoverColor to whatever it wants, and your inherited Style can override it, or you can override it directly on the element in your XAML. I'm not providing code samples for this approach (right now, unless requested) since this is a more commonly-used approach that I'm guessing you're already familiar with.</p> <p>The <strong>second approach</strong> would be to define a <strong>custom attached property</strong>. I've not seen this approach used as much for dealing strictly with Styling issues, perhaps because for the attached "behavior" to fully do its job in this case, authors have used Style files to react (bind to) and apply visual changes based on the attached property, rather than the code in the attached property changed callback doing something stylistically (but I suppose it could still be done that way). However, this approach feels "lighter weight" to many, since you don't need to subclass any existing controls. </p> <p>An example of this second approach can be found in the MahApps.Metro library, specifically the <a href="https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Controls/TextboxHelper.cs" rel="nofollow"><strong>TextboxHelper class</strong></a> (which houses attached properties) and the <a href="https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Controls.TextBox.xaml" rel="nofollow"><strong>Controls.TextBox.xaml style file</strong></a> (which binds to those attached properties). For example, we see in the control template for the TextBox, this line that makes use of the Watermark attached property:</p> <pre><code>&lt;TextBlock x:Name="Message" Text="{TemplateBinding Controls:TextboxHelper.Watermark}" Visibility="Collapsed" Foreground="{TemplateBinding Foreground}" IsHitTestVisible="False" Opacity="0.6" HorizontalAlignment="Left" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="6,0,0,0"/&gt; </code></pre> <p>Now you can imagine that you could set that Watermark value in a base style to something:</p> <pre><code>&lt;Setter Property="Controls:TextboxHelper.Watermark" Value="My helpful watermark for all!"/&gt; </code></pre> <p>And then override it in an inherited style:</p> <pre><code>&lt;Setter Property="Controls:TextboxHelper.Watermark" Value="A more specific watermark!"/&gt; </code></pre> <p>With either approach, we can define any "variable" we want and easily set them in a Style setter, override them in inherited styles, <code>TemplateBind</code> to them within control templates, or <code>Trigger</code> off of them.</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