4

Is there any way that I change a Label's behavior to support toggling by click in WPF?

i.e. that's Selector.IsSelected property toggle between "True" and "False" by clicking?

Regards.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
amiry jd
  • 27,021
  • 30
  • 116
  • 215

4 Answers4

9
<StackPanel>
    <CheckBox IsChecked="{Binding IsChecked, ElementName=checkbox}" Content="Hello">
        <CheckBox.Template>
            <ControlTemplate TargetType="CheckBox">
                <ContentPresenter/>
            </ControlTemplate>
        </CheckBox.Template>
    </CheckBox>
    <CheckBox x:Name="checkbox" Content="A normal checkbox"/>
</StackPanel>

Note that the above template does not alter the appearance of the label based on whether it's checked or not. That might be something you'll need - hard to say without more information.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 1
    That's right dear Kent; I need an idea to do this, and your code give me that (^_^); My problem resolved. Regards. – amiry jd May 28 '11 at 16:38
2

You would better use Checkbox for such behaviour and style it to your liking.

Remember controls in WPF represent behaviours not looks.

Kugel
  • 19,354
  • 16
  • 71
  • 103
1

Just want to add the way more simple solution of mikelt21 in the duplicate post:

<CheckBox>
    <Label Content="Your text here"/>
</CheckBox>
SpeziFish
  • 3,262
  • 2
  • 28
  • 27
0

The simplest thing I can think of is:

<CheckBox Content="Text goes here"/>

It is built-in behavior and if you are happy with the placement of the text (at the right of the CheckBox at least in my locale) there's no reason to make it more complicated.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81