WPF Datagrid 单元格、单元格信息和选定单元格 + 自定义选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17066320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid cell, cellinfo and selectedcells + custom selection
提问by Ms. Nobody
I want to manipulate selection in WPF datagrid, but I have problem with access to actual cells and setting focus on them and marking them as selected.
我想在 WPF 数据网格中操作选择,但我在访问实际单元格和设置焦点并将它们标记为选中时遇到问题。
Can anyone explain me: Why there isn't some simple way to get the **DatagridCell** from **DatagridCellInfo**?- Why is almost nobody on SO working with WPF datagrids? (I don't see much Q/A with votes up)
- Is there an easy way how to make your own selection mode for WPF datagrid?
谁能解释一下:为什么没有一些简单的方法可以从 **DatagridCellInfo** 获取 **DatagridCell**?- 为什么几乎没有人使用 WPF 数据网格?(我没有看到太多的 Q/A 投票)
- 有没有一种简单的方法可以为 WPF 数据网格创建自己的选择模式?
What's my problem
我有什么问题
I wanted to make custom selectionon WPF Datagrid when selecting more cells (one by one) without pressing Ctrl. I did it quite good but I'm having problems when I want to deselect one of selected cells - by just simply clicking on it. It's not a problem to remove it from the list. Problem is that When it's clicked on it takes focus and is hilighted and all others that were selected turn off their hilight. If I select another cell that wasn't selected all the selected cells get hilighted again correctly. The problem is only in the deselection.
我想在不按 Ctrl 的情况下选择更多单元格(一个接一个)时在 WPF Datagrid 上进行自定义选择。我做得很好,但是当我想取消选择一个选定的单元格时遇到了问题 - 只需单击它。从列表中删除它不是问题。问题是当它被点击时,它会获得焦点并被高亮显示,而所有其他被选中的人都会关闭它们的高亮显示。如果我选择另一个未被选中的单元格,所有选定的单元格都会再次正确突出显示。问题仅在于取消选择。
My code:
我的代码:
XAML:
XAML:
<Window x:Class="SelectionTest.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">
<Window.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid
Name="mydatagrid"
Width="Auto" Height="Auto"
HeadersVisibility="All"
AutoGenerateColumns="True" IsReadOnly="True"
SelectionMode="Extended" SelectionUnit="Cell"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserReorderColumns="False" CanUserSortColumns="False"
SelectedCellsChanged="mydatagrid_SelectedCellsChanged"
Padding="10" HorizontalAlignment="Center" VerticalAlignment="Top"
>
</DataGrid>
</Grid>
</Window>
I have filled the datagrid with list of some random example class objects I made.
我已经用我制作的一些随机示例类对象的列表填充了数据网格。
C#:
C#:
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
DataGridCellInfo cellInfo = new DataGridCellInfo(cell);
if ((cell.IsSelected)||(selectedList.Contains(cellInfo))||(selectedCellsList.Contains(cell)))
{
selectedList.Remove(cellInfo);
selectedCellsList.Remove(cell);
cell.IsSelected = false;
mydatagrid.CurrentCell = selectedList[0];
}
else
{
if (selectedList.Count < 7)
{
selectedList.Add(cellInfo);
selectedCellsList.Add(cell);
}
else
{
selectedList.RemoveAt(0);
selectedList.Add(cellInfo);
selectedCellsList.RemoveAt(0);
selectedCellsList.Add(cell);
}
}
mydatagrid.SelectedCells.Clear();
mydatagrid.UnselectAll();
foreach (DataGridCell xcell in selectedCellsList)
{
xcell.IsSelected = true;
xcell.Focus();
}
}
If this code looks really ugly to you, then I'm sorry. But I'm still just a little csharpawan.
如果这段代码在你看来真的很难看,那么我很抱歉。但我仍然只是一个小csharpawan。
What's my problem in shortcut:Clicking on selected cell makes only it hilighted and focused and dehilights all other selected cells which is exact opposite what I want it to do. (If I click other not yet selected cell it works the way I want it.)
我的快捷方式有什么问题:单击选定的单元格只会使其突出显示和聚焦,并取消突出显示所有其他选定的单元格,这与我想要它做的完全相反。(如果我单击其他尚未选择的单元格,它会按照我想要的方式工作。)
回答by Richard E
Answer to question 1: A quick way to get DataGridCell from DataGridCellInfo:
问题 1 的答案:从 DataGridCellInfo 中获取 DataGridCell 的快速方法:
public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent != null)
return (DataGridCell) cellContent.Parent;
return null;
}

