Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set BackColor of a CheckBox in WPF

Tags:

wpf

In WinForm we can set BackColor of a CheckBox

enter image description here

How do I do this in WPF? I try

<CheckBox Content="CheckBox" Background="Red"/>

but this only changes the rectangle border color

enter image description here

I also try

<CheckBox>
    <TextBlock Text="CheckBox" Background="Red"/>
</CheckBox>

but this only changes the text background color, not including the rectangle

enter image description here

=======

Thank you all for the solutions. I think the simplest way works for me :)

like image 542
walterhuang Avatar asked Mar 23 '15 22:03

walterhuang


3 Answers

If you experiment a little with panels you get there:

<Grid Background="Red" HorizontalAlignment="Left">
  <CheckBox Content="test" />
</Grid>

is pretty close to what you want. Tried it myself ;-)

like image 148
Philip Stuyck Avatar answered Sep 27 '22 00:09

Philip Stuyck


<Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <CheckBox FlowDirection="RightToLeft" background="Red"
                          IsChecked="False" />
                <Border Grid.Column="1"
                        Margin="20 0 0 0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        Background="LightGray">
                    <TextBlock HorizontalAlignment="Left"
                               Foreground="Black"
                               Style="{StaticResource RegularTextblock}"
                               Text="Checkbox1" />
                </Border>
            </Grid>
like image 35
Jayasri Avatar answered Sep 26 '22 00:09

Jayasri


You are asking for little too much. Using Blend I made created a relevant style for the CheckBox.

The code was too big. So SO did not allow to display. Here is the pastie link

For the Background there is a grid markGrid. I added a Background and a TemplateBinding to force the CheckBox to change the colour. The drawback is the Path colour will be visible very faintly if the background is dark.

enter image description here

like image 24
Sandesh Avatar answered Sep 25 '22 00:09

Sandesh