D
Dave Weeden
Hi everyone,
I've run into a strange problem when prompting the user to confirm whether
they want to clear a checkbox.
It seems that showing a MessageBox during a PreviewMouseLeftButtonDown event
seems to disrupt normal operation of a checkbox in that it will no longer
uncheck itself.
I found a workaround (comment out my hack and see what happens) but am
wondering if this is symptomatic of something bad. Does anyone know?
Thanks,
Dave
Sample XAML
----------------
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<CheckBox
PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown">Click
Me</CheckBox>
</Window>
Associated code
------------------
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Sample
{
public partial class Window1 : Window
{
private const bool SHOW_PROMPT = true;
public Window1()
{
InitializeComponent();
}
private void CheckBox_PreviewMouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.IsChecked.GetValueOrDefault() && SHOW_PROMPT)
{
if (MessageBox.Show("Are you sure you want to uncheck me?",
"Prompt", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
MessageBoxResult.Yes)
{
// HACK
checkbox.IsChecked = false;
}
else
{
// handle event
e.Handled = true;
}
}
}
}
}
I've run into a strange problem when prompting the user to confirm whether
they want to clear a checkbox.
It seems that showing a MessageBox during a PreviewMouseLeftButtonDown event
seems to disrupt normal operation of a checkbox in that it will no longer
uncheck itself.
I found a workaround (comment out my hack and see what happens) but am
wondering if this is symptomatic of something bad. Does anyone know?
Thanks,
Dave
Sample XAML
----------------
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<CheckBox
PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown">Click
Me</CheckBox>
</Window>
Associated code
------------------
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Sample
{
public partial class Window1 : Window
{
private const bool SHOW_PROMPT = true;
public Window1()
{
InitializeComponent();
}
private void CheckBox_PreviewMouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.IsChecked.GetValueOrDefault() && SHOW_PROMPT)
{
if (MessageBox.Show("Are you sure you want to uncheck me?",
"Prompt", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
MessageBoxResult.Yes)
{
// HACK
checkbox.IsChecked = false;
}
else
{
// handle event
e.Handled = true;
}
}
}
}
}