WPF click on label change checkbox isChecked property [duplicate]

↘锁芯ラ 提交于 2019-12-01 16:19:35
Danield

Paste this code into kaxaml

You'll see that clicking on the label toggles the checkbox.

[See this SO answer by Kent]

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <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>
</Page>

You can also do this:

<CheckBox>
    <Label Content="Your text here"/>
</CheckBox>

One limitation though is that the text will have to be on the right side of the checkbox.

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