【发布时间】:2012-08-24 15:07:03
【问题描述】:
我正在构建一个 POS 应用程序,并且我希望最终用户能够对数据网格(即 I.E.)进行切换选择模式。他们可以单击多行,并且每个单击的项目都将累积在 SelectedItems 属性上 - 同样单击已选择的行将取消选择该行。我在另一个 stackoverflow 问题中找到了这段代码:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DoCheckRow" />
</Style>
</DataGrid.Resources>
public void DoCheckRow(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null && !cell.IsEditing)
{
DataGridRow row = VisualHelpers.TryFindParent<DataGridRow>(cell);
if (row != null)
{
row.IsSelected = !row.IsSelected;
e.Handled = true;
Debug.WriteLine(sender);
}
}
}
这实际上给了我想要的切换选择模式,但是,当我将按钮添加为 CellTemplate 时,单击时不会触发按钮命令,因为我在上面的代码中设置了 e.Handled = true;这停止了事件泡沫。有什么办法可以兼顾两者吗?
【问题讨论】:
标签: wpf events toggle wpfdatagrid