question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WinUI3 Button XAML Template does not respect Foreground property after mouse passes over

See original GitHub issue

Describe the bug

In WInUI 3, create a Button with:

<Button Name="Undo1" Content="Undo." Background ="DarkSlateBlue" Foreground ="White"/> On starting the app, the colors are correct, but after moving the mouse over the button, the text color will remain black rather that white as specified with Foreground property. ice_video_20220608-182052_edit_0

Steps to reproduce the bug

as above.

Note that the key issue is that the default template for Button (inside generic.xaml) does not have a templatebinding for Foreground, but includes Background. My reference for the template is: C:\Users.….nuget\packages\microsoft.windowsappsdk\1.0.3\lib\net5.0-windows10.0.18362.0\Microsoft.WinUI\Themes

The template contains:

      <Setter Property="Background" Value="{ThemeResource ButtonBackground}" />

It should contain:

      <Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
      <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />

Expected behavior

Foreground color property should be reset to original color after the mouse has left the button zone. It will do that if the generic template is updated as suggested

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.0.3

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 10 (21H2): Build 19044

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
gabbybilkacommented, Dec 15, 2022

🎉Microsoft.WindowsAppSDK.1.2.221209.1 has been released which fixes this issue.

Handy links:

2reactions
ranjeshjcommented, Sep 30, 2022

In this particular case, there is a template bind to the foreground property for the control. The foreground property is inherited by the ContentPresenter in the template. The PointerOver visual state is writing the foreground property and when going back to normal, the inhertited property is not going back to its original value. A workaround would be to explicitly set the foreground property on the ContentPresenter to a template binding instead of it getting inherited. That unfortunately needs a re-template. Testing a fix to WinUI3’s button template.

@michael-hawker There are other cases where the properties really need to be template bound instead of pointing to theme resources like you mention in your linked bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Button Class (Microsoft.UI.Xaml.Controls) - Windows
The following example shows three buttons that respond to clicks in three different ways based on their ClickMode property value. Hover - When...
Read more >
c# - Changing the foreground of button text and image on ...
So far, I have the following style, which doesn't work. It changes the foreground of the textblock and the definition of the BlackBrush...
Read more >
Buttons in the hover state ignore the Foreground property ...
But, in fact when I put my mouse on it, it stays white as defined in "ResourceKey=Foreground". It looks like the trigger is...
Read more >
Untitled
The button control can be customized using the following properties: Text Customization The text inside the button can be customized by its text...
Read more >
XAML Brewer, by Diederik Krols | If you're not having fun, you ...
It provides mapping support for Microsoft's XAML platforms WPF, UWP, and WinUI 3. Here's a screenshot from the small sample app in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found