walterhuang
walterhuang

Reputation: 632

How to set BackColor of a CheckBox in 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 :)

Upvotes: 8

Views: 9523

Answers (3)

Jayasri
Jayasri

Reputation: 235

<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>

Upvotes: 4

Sandesh
Sandesh

Reputation: 3004

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

Upvotes: 1

Philip Stuyck
Philip Stuyck

Reputation: 7467

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 ;-)

Upvotes: 11

Related Questions