我想达到的效果:
简单,对吧? ; - ;
感谢。
答案 0 :(得分:3)
不太难。
代码背后
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Checkbox_OnMouseEnter(object sender, MouseEventArgs e)
{
var checkbox = sender as CheckBox;
if (e.LeftButton == MouseButtonState.Pressed)
{
if (checkbox != null)
{
checkbox.IsChecked = !checkbox.IsChecked;
}
}
}
private void UIElement_OnGotMouseCapture(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var checkbox = sender as CheckBox;
if (checkbox != null)
{
checkbox.IsChecked = !checkbox.IsChecked;
checkbox.ReleaseMouseCapture();
}
}
}
<强> XAML 强>
<Window x:Class="ClickAndDrag.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
<CheckBox MouseEnter="Checkbox_OnMouseEnter" GotMouseCapture="UIElement_OnGotMouseCapture"/>
</StackPanel>
释放鼠标捕获的原因是为了防止复选框在单击时吞下所有事件。