Change a Label's behavior to support toggling by click in WPF

Deadly 提交于 2019-11-28 02:06:31
Kent Boogaart
<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.

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

Remember controls in WPF represent behaviours not looks.

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

<CheckBox>
    <Label Content="Your text here"/>
</CheckBox>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!