Search code examples
wpfwpf-controlslabeltoggle

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


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.


Solution

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