0

In our WPF application, we have a custom listbox that when unstyled, matches the default Windows theme for things like the highlighted, hovered or selected items (i.e. nice blue translucent gradient.)

However, when we try creating our own ListBoxItem template and use values like in this code...

<Trigger Property="Selector.IsSelected" Value="True">
    <Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>

...the control loses all traces of the Aero theme reverting to the old Win95/Classic look. How can we say 'Hey... apply the theme's 'highlighted' style to our border.'?

Again, the resources are obviously loaded as they're there until we re-template the control, but how are we supposed to access the built-in styles of the theme? After all, that 'selection' look is all over the place... ListBox, ComboBox, ListView, everywhere. We just want it in our control too without having to reinvent the wheel.

Note: We used the ShowMeTheTemplate.exe app to get the default 'Aero' style, but again, that's not what we're seeing when we run it.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

2 Answers2

0

Your answer appears to be here:

....you should use ItemsContainerStyle instead of just defining a replacement ItemTemplate...and define your Style using BasedOn to inherit the default style defined for your FrameworkElement element by the theme.

There are also some other techniques you can use to leverage the default style defined by a theme (...just gives you extra options).

And yet another technique not covered above that uses a markup extension to merge together some styles (i.e. you could merge the default style and your style).

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • The problem I see here is that all of these in one way or another are based on the overall style of say, `ListBoxItem`, but that's not what I'm after. Inside the template for the `ListBoxItem` there's a border that has a blue gradient background when using Aero and the item is selected. *That* is what I'm after... the background to apply to the border, not the overall template for the `ListBoxItem`. You can't simply replace the template either as the triggers that set the background styles are also part of the template. Hope that makes sense. – Mark A. Donohoe Aug 26 '12 at 18:06
  • Look at the ListBoxItem template again in ShowMeTheTemplate....you've missed the bit lower down with the MultiDataTrigger on the Selector.IsSelected, etc, which modifies the Border "Bd".... – Colin Smith Aug 26 '12 at 18:12
  • I'm actually very familiar with that tool and have its code base as part of our solution under a 'Utilities' folder exactly for things like this, but this *still* doesn't explain what I'm after. I am looking for the style of the border *within* the template. You can't replace a sub-part of a template, so I would have to copy the entire thing, let alone create one per theme. I just want the border and background that the theme already supplies. Hope that makes more sense. – Mark A. Donohoe Jan 02 '13 at 08:11
  • Maybe this will help you ... http://stackoverflow.com/questions/5736989/highlightbrushkey-settings-not-working-in-windows-7 – Colin Smith Jan 02 '13 at 11:35
  • Again, I'm very well versed in those and that's not what I'm asking. I say that because that still doesn't explain why I can't use the same *KEY* for the background brush since you can change the contents of that key dynamically. In other words, in their example, the line '{DynamicResource {x:Static SystemColors.HighlightBrushKey}}' should refer to the highlight brush whether it's based on a solid, a gradient or whatever, again, regardless of the theme. But it doesn't. It seems to always ponit to the solid brush. If I want the gradient version, I have to explicitly define it per-theme. – Mark A. Donohoe Jan 02 '13 at 16:22
0

You can use BasedOn="{StaticResource {x:Type ItemType}}" in your Style to modify the existing one however there is no way to merge part of a default template with your custom template, templates are monolithic.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • What if I didn't want to merge them, but rather just gain access to the internals? For instance, every selectable thing in a WPF theme basically has the same 'IsSelected' look whether a ListBox, ComboBox, TreeView, etc. (Ok, so a textbox's selection is different but I digress.) All of them have a common look to the border named 'Bd' in the templates. Does this mean although they are *all* identical, they are also all independent copies of the same thing, and thus, I'd have to create my own completely independent copy? – Mark A. Donohoe Jan 02 '13 at 08:14
  • Yes, they are independent. Further you should view the template as a blackbox, the default template is OS-dependent so you have no way to know what actually will be in the template at runtime. This is why you should not access controls inside the template even though you could in theory. – H.B. Jan 05 '13 at 10:53
  • My only issue with that is that WPF *does* define things like selection highlight brush, highlight forecolor, etc., but their own default styles don't appear to use them. It's a shame. For common things like that, they should have exposed those as presented by the theme. That would lend itself to a more consistent visual user experience. What's the point of defining such values globally if they don't actually reflect what the true global value is? More rhetorical than anything else there so no need to reply, but I think MS made an error in judgment there hiding it all. – Mark A. Donohoe Jan 06 '13 at 22:03
  • Some default styles use those by referencing the system color keys ([e.](http://stackoverflow.com/a/1278299/546730)), maybe the usage is inconsistent though... – H.B. Jan 06 '13 at 22:25
  • Yeah, I get the keys usage. That's what I'm talking about... at least let us set up dynamic resources keyed off of those global keys, but they don't even do that. Frustrating since it means we have to exactly recreate something already created simply because it isn't exposed... and we have to do that once per theme. I guess the other approach is to ignore the themes altogether and just go with a look unique unto yourself, just like Photoshop, or even the latest VS does now. – Mark A. Donohoe Jan 06 '13 at 23:08