【发布时间】:2014-05-26 16:06:52
【问题描述】:
我正在尝试实现单击行为以在 DataGrid 列中设置 CheckBoxes。这可行,但我只想在光标位于复选框上方时切换 IsChecked 属性。目前,只要 DataGridCell 获得焦点,我就会切换。
我找不到确定鼠标光标是否在复选框上方的方法...
private void dataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell focusedCell = e.OriginalSource as DataGridCell;
if (focusedCell != null)
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
Control editControl = focusedCell.Descendants<Control>().FirstOrDefault();
if (editControl != null)
{
CheckBox checkBox = editControl as CheckBox;
if (checkBox != null)
{
if (checkBox.IsEnabled)
{
checkBox.Focus();
checkBox.SetCurrentValue(ToggleButton.IsCheckedProperty, !checkBox.IsChecked);
var bindingExpression = checkBox.GetBindingExpression(CheckBox.IsCheckedProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
}
else
{
dataGrid.Dispatcher.BeginInvoke(new Action(() => editControl.Focus()), DispatcherPriority.ContextIdle);
}
}
}
}
有什么想法我在这里做错了吗?
最好的问候 去吧
【问题讨论】: