1

I have a WPF app with the follow XAML

<CheckBox IsChecked="{Binding IsTime}" />
<TextBlock Text="Time" />

What I'd like is if the user clicks the textbox, it will toggle the CheckBox. In the same way we can use label and a checkbox in MVC.NET (well, HTML)

I could do this with events in the code behind but I'm using MVVM and as such, don't want to use those events.

The difference between my question and Change a Label's behavior to support toggling by click in WPF is that I'm already binding my CheckBox to something...

I hope this effort provides a clearer idea on what I'm trying to do

<CheckBox IsChecked="{Binding IsTime}" x:Name="Checky" />
<TextBlock Text="Time">
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="TextBlock.MouseDown" Value="True">
                    <Setter TargetName="Checky" Property="IsChecked" Value=Not IsTime> //WHAT TO DO HERE
                    </Setter>
                </Trigger>
             </Style.Triggers>
         </Style>
   </TextBlock.Style>
</TextBlock>
Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

2 Answers2

2

You can put the TextBox and other controls inside the CheckBox. This way clicking the TextBox will also toggle the CheckBox. Here's an example with multiple items inside the CheckBox, but you could of course have only the TextBox inside it.

<CheckBox Padding="2" IsChecked="{Binding IsTime}">
    <StackPanel Orientation="Horizontal">
        <Image Width="24" Height="24" Source="someimage"/>
        <TextBlock Text="Time"/>
    </StackPanel>
</CheckBox>
Jonas Kivi
  • 51
  • 3
0

Use the code in the link you have provided and bind IsChecked to the same property:

<StackPanel>
    <CheckBox x:Name="checkbox" IsChecked="{Binding IsTime}"/>

    <CheckBox IsChecked="{Binding IsTime}" Content="Hello">
        <CheckBox.Template>
            <ControlTemplate TargetType="CheckBox">
                <ContentPresenter/>
            </ControlTemplate>
        </CheckBox.Template>
    </CheckBox>
</StackPanel>

You know that you have a Content property on the CheckBox which can be templated to achieve the desired effect without having to use multiple CheckBox-es.

Novitchi S
  • 3,711
  • 1
  • 31
  • 44