简体   繁体   中英

c# WPF Change the check color of a CheckBox

 <CheckBox Background="White" IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"/>

I need to change the color of the check. I figured Forground did this since Background changed the background color but to no avail.

Any ideas? Surly there is a way to directly change the check color. I tried googling this but the only solution I found was to make my own checkbox class.

Where is the property to change the CheckBox Check color?

I found one answer with

    <Path Visibility="Collapsed"
      Width="7"
      Height="7"
      x:Name="CheckMark"
      SnapsToDevicePixels="False"
      StrokeThickness="2"
      Data="M 0 0 L 7 7 M 0 7 L 7 0">
  <Path.Stroke>
    <SolidColorBrush Color="{DynamicResource GlyphColor}" />
  </Path.Stroke>
</Path>

But that does not work since I cannot add that as a child element. Even if it worked, it would change too much. All I want is a white background with a black checkmark. Grey on Grey is terrible looking. Isn't there a built in way to change the color?

Unfortunately, the WPF folks decided to make the checkbox a BEAST to deal with. The check itself is actually a "BulletChrome" control, if you study the template. Now, the problem is that the BulletChrome control is all written in C# and not customizable at all from the outside, not via XAML, so all the pens and brushes are hard-coded . You'll find with Reflector or IlSpy, that you are after the GlyphStroke and GlyphFill properties and those two properties clone internal hardcoded brushes.

I had to do the same thing you did, and I ended up just copying the entire class out using ILSpy and modifying it just to tweak a few things.

Not the answer you'd hoped for, but there isn't a way to override this control easily.

EDIT: well, you could modify the two brushes with reflection if you want. That's easier then copying the whole class out :).

Its a beast of a class, so... BulletChrome.cs = 2262 lines!

EDIT #2: btw, doing a quick 1 or 2 line hack will break the animations, so you need to go the correct route if you care about those :).

EDIT #3: don't forget there are quite a few states for the check mark. Hover, pressed, disabled, etc. and lots of animations and transitions. So anybody who gives you a quick answer doesn't understand how this control is put together.

This is as annoying as SledgeHammer said with the Bullet bs. basically, the brushes are hardcoded, so there isn't an easy solution. That being said, Here is a quick 1 line hack. This doesn't break animations on my machine.

Though, as SledgeHammer pointed out, there are several different states. I'll leave it up to you to override which ones you need. This will do the basic check.

typeof(BulletChrome).GetRuntimeFields().Where(f => f.Name == "_commonCheckMarkFill").First().SetValue(null, new SolidColorBrush(Colors.Yellow));

You're also going to have to reference PresentationFramework.Aero and include using Microsoft.Windows.Themes;

我机器上的例子

Its worth pointing out, this will affect every CheckBox , and potentially (depending on which brushes you overwrite) RadioButton , so depending on your needs, YMMV. But it does what you asked for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM