mon*_*str 4 wpf datagrid mouseleftbuttondown
我有一个共同的任务。一键实现DataGrid中的CheckBox勾选。我决定创建一个从 DataGrid 派生的 DataGridExtended 类,并实现类似的东西:
XAML:
<DataGrid x:Class="DataGrid.DataGridExtended"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
代码:
public partial class DataGridExtended : System.Windows.Controls.DataGrid
{
private int _oldRowIndex;
private int _oldColumnIndex;
public DataGridExtended()
{
MouseLeftButtonUp += DataGridExtendedMouseLeftButtonUp;
MouseLeftButtonDown += DataGridExtendedMouseLeftButtonDown;
}
private void DataGridExtendedMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// ???? ?????? ??????? DataGridExtended
var dataGridExt = sender as DataGridExtended;
if (dataGridExt == null)
return;
// ???????? ??????? ??????
var currentCell = dataGridExt.CurrentCell;
_oldRowIndex = dataGridExt.SelectedIndex;
_oldColumnIndex = dataGridExt.CurrentColumn.DisplayIndex;
}
private void DataGridExtendedMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// ???? ?????? ??????? DataGridExtended
var dataGridExt = sender as DataGridExtended;
if (dataGridExt == null)
return;
var rowIndex = dataGridExt.SelectedIndex;
var columnIndex = dataGridExt.CurrentColumn.DisplayIndex;
// ???????? ??????? ??????
var currentCell = dataGridExt.CurrentCell;
//if (_oldRowIndex != rowIndex || _oldColumnIndex != columnIndex)
// return;
// ???????? ??????? ???????
var currentColumn = currentCell.Column;
// ???????? ??????? ??????? ??????
var cellContent = currentColumn.GetCellContent(currentCell.Item);
// ???? ???????? ?? ????????
var checkBox = cellContent as CheckBox;
if (checkBox == null)
return;
// ??????? ??? ? ?????
checkBox.Focus();
// ?????? ??? ?? ???????????????
checkBox.IsChecked = !checkBox.IsChecked;
// ???????? ????????? ???????? ??? ????????
var bindingExpression = checkBox.GetBindingExpression(ToggleButton.IsCheckedProperty);
// ???? ???????? ???? - ????????? ??
if (bindingExpression != null)
bindingExpression.UpdateSource();
}
}
Run Code Online (Sandbox Code Playgroud)
DataGridExtendedMouseLeftButtonUp 处理程序工作正常,但 DataGridExtendedMouseLeftButtonDown 不会触发。这就是问题所在。
如果没有 DataGridExtendedMouseLeftButtonDown 调用,检查行为不是我想要的。即,即使我将光标从网格中移出,检查也能正常工作:E 尝试使用 PreviewMouseLeftButtonDown 而不是 MouseLeftButtonDown 会产生错误的效果 :(
那么,我该如何解决我的问题?不要提供使用不同的方法来实现一键检查 plz :) 例如使用 XAML 样式...
在 WPF 中,我们经常遇到特定Click处理程序似乎不起作用的情况。这样做的原因通常是因为控件(或我们自己的代码)正在处理该事件和设置e.Handled = true;,这会阻止事件进一步传递。在这些情况下,通常认为您应该在发生这种情况之前尝试访问事件,因此我们转向匹配/相关Preview事件。
在您的情况下,我建议您使用该PreviewMouseLeftButtonDown事件。你说到那时某些东西还没有初始化,但这对我来说没有任何意义。你说你需要保存以前的值,但你可以只从你的DataGridExtendedMouseLeftButtonUp事件处理程序中做到这一点。
当用户第一次释放鼠标按钮时,您就拥有了他们的新值。将其保存在变量中。当用户在下一次和随后的每次释放鼠标按钮时,将他们从变量中的先前值保存为旧值,然后将他们的新值读入变量中。