-2

Created a simple checkbox in WPF. The single click toggles the checkbox status in the UI. I want the checkbox status to be changed when I double click on checkbox text. Please help me with code below.

The only control in the xaml:

<Grid>
    <CheckBox Content="CheckBox" HorizontalAlignment="Left" Height="60" Margin="144,93,0,0" VerticalAlignment="Top" Width="392"/>
</Grid>
Bulutay Saraç
  • 644
  • 9
  • 20
S_1
  • 62
  • 5

2 Answers2

2

I could not find a solution with standart CheckBox Control. I've created my own checkBox via UserControl.

DoubleClickCheckBox.xaml (UserControl)

<Grid VerticalAlignment="Center" HorizontalAlignment="Center" Loaded="Grid_Loaded">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <CheckBox Name="checkBox" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
    <TextBlock Name="textBlock" MouseDown="TextBlock_MouseDown" TextAlignment="Center" Grid.Column="1" Margin="4,0,4,0"/>
</Grid>

DoubleClickCheckBox.xaml.cs (UserControl)

public partial class DoubleClickCheckBox : UserControl
{
    public string Text { get; set; }

    public bool IsChecked { get; set; }

    public event EventHandler UserControlChecked;

    public event EventHandler UserControlUnChecked;

    public DoubleClickCheckBox()
    {
        InitializeComponent();
    }

    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        this.checkBox.IsChecked = this.IsChecked;
        this.textBlock.Text = this.Text;
    }

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if(e.ClickCount == 2)
            this.checkBox.IsChecked = !this.checkBox.IsChecked;
    }

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        if (UserControlChecked != null)
            UserControlChecked(this, EventArgs.Empty);
    }

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        if (UserControlUnChecked != null)
            UserControlUnChecked(this, EventArgs.Empty);
    }
}

MainWindow.xaml

<Grid>
    <local:DoubleClickCheckBox VerticalAlignment="Center" HorizontalAlignment="Center" Background="Aqua" Text="Test" IsChecked="False" UserControlChecked="DoubleClickCheckBox_UserControlChecked" UserControlUnChecked="DoubleClickCheckBox_UserControlUnChecked"/>
</Grid>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void DoubleClickCheckBox_UserControlChecked(object sender, EventArgs e)
    {
        this.Title = "Checked";
    }

    private void DoubleClickCheckBox_UserControlUnChecked(object sender, EventArgs e)
    {
        this.Title = "UnChecked";
    }
}

You can customize everything with this style. I customized only checked and unchecked events. It is up to you in future.

enter image description here

EDIT: As post owner wanted, i enabled single click again. Removed CheckBox_PreviewMouseButtonDown event.

Bulutay Saraç
  • 644
  • 9
  • 20
  • Thank You . This is working perfectly as i needed .But in addition I want the status toggle in single click as well. So ,both in case of double click and single click , I want the status change . – S_1 Sep 23 '19 at 07:12
  • The only thing we need to change is the CheckBox_PreviewMouseButtonDown event. I disabled single click in this event. I'm gonna edit the solution. – Bulutay Saraç Sep 23 '19 at 09:10
-1

If you just want to toggle the checkbox status when double clicked, then simply make an CheckBox_MouseDoubleClick Event and set the IsChecked property to true. But take care, this Solution is not within the MVVM pattern.

Warsox
  • 63
  • 9
  • Checking or unchecking via user input is a view responsibility. Thus handling an event like this isn't breaking any mvvm rules. If there was such an event. – Andy Sep 18 '19 at 11:33