- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要获取在 DataGrid 单元格更改事件中获得的 WPF DataGridCell 的位置,但只能获取垂直(Y 轴)。
尽管指向了不同的列,但水平方向保持不变。
这是几乎可以工作的代码。
通过单击不同的单元格进行测试。
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<Person> Persons = new List<Person>();
public MainWindow()
{
InitializeComponent();
Persons.Add(new Person { Id = 1, Name = "John", City = "London" });
Persons.Add(new Person { Id = 2, Name = "Charles", City = "Rome" });
Persons.Add(new Person { Id = 3, Name = "Paul", City = "Chicago" });
this.EditingDataGrid.ItemsSource = Persons;
this.EditingDataGrid.CurrentCellChanged += new EventHandler<EventArgs>(EditingDataGrid_CurrentCellChanged);
}
void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCell Cell = GetCurrentCell(this.EditingDataGrid);
var Position = Cell.PointToScreen(new Point(0, 0));
// WHY X NEVER CHANGES??!!
MessageBox.Show("X=" + Position.X.ToString() + ", Y=" + Position.Y.ToString(), "Position");
}
/// <summary>
/// Returns, for this supplied Source Data-Grid, the current Data-Grid-Cell.
/// May return null if no associated Cell is found.
/// </summary>
public static DataGridCell GetCurrentCell(DataGrid SourceDataGrid)
{
if (SourceDataGrid.CurrentCell == null)
return null;
var RowContainer = SourceDataGrid.ItemContainerGenerator.ContainerFromItem(SourceDataGrid.CurrentCell.Item);
if (RowContainer == null)
return null;
var RowPresenter = GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(RowContainer);
if (RowPresenter == null)
return null;
var Container = RowPresenter.ItemContainerGenerator.ContainerFromItem(SourceDataGrid.CurrentCell.Item);
var Cell = Container as DataGridCell;
// Try to get the cell if null, because maybe the cell is virtualized
if (Cell == null)
{
SourceDataGrid.ScrollIntoView(RowContainer, SourceDataGrid.CurrentCell.Column);
Container = RowPresenter.ItemContainerGenerator.ContainerFromItem(SourceDataGrid.CurrentCell.Item);
Cell = Container as DataGridCell;
}
return Cell;
}
/// <summary>
/// Returns the nearest child having the specified TRet type for the supplied Target.
/// </summary>
public static TRet GetVisualChild<TRet>(DependencyObject Target) where TRet : DependencyObject
{
if (Target == null)
return null;
for (int ChildIndex = 0; ChildIndex < VisualTreeHelper.GetChildrenCount(Target); ChildIndex++)
{
var Child = VisualTreeHelper.GetChild(Target, ChildIndex);
if (Child != null && Child is TRet)
return (TRet)Child;
else
{
TRet childOfChild = GetVisualChild<TRet>(Child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
<DataGrid x:Name="EditingDataGrid"/
>
最佳答案
您可以获得 DataGridCell
从 CurrentCell 这样
void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCell Cell = GetDataGridCell(EditingDataGrid.CurrentCell);
var Position = Cell.PointToScreen(new Point(0, 0));
MessageBox.Show("X=" + Position.X.ToString() + ", Y=" + Position.Y.ToString(), "Position");
}
public static DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
if (cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
DataGrid
上创建扩展方法。去做这个
public static class DataGridExtensions
{
public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)
{
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if (cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
}
DataGridCell
时都可以这样使用
DataGridCell Cell = EditingDataGrid.GetCurrentDataGridCell();
关于wpf - 如何获得 WPF DataGridCell 视觉水平(X 轴)位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5287137/
我有 DataGrid 并且其中一个 DataGrid 列看起来像这样 问题是我被迫两次使用 BooleanToY
我如何知道 DataGridCell 当前是否处于编辑模式(不是 IsSelected),我的意思是,例如,单击 DataGridTextColumn 单元格它变成了 TextBox 而不是 Text
我有一个 DataGrid 并且在点击时我想找到被点击单元格的索引。我找到了获取 DataGridCell 和 DataGridCellInfo 的方法,但其中只有列索引。 如何获取行索引?我找不到获
如果左侧单元格的内容大于其宽度,我如何使用 DataGrid 中右侧的空单元格来显示连续文本? 如果右边单元格有数据,可以使用TextTrimming="CharacterEllipsis" ----
我有一个包含动态数据(自定义数据行的集合)的 DataGrid,我从服务器获取这些数据。 DataRow 有一个索引器和一个属性 Data,它返回绑定(bind)的整个数据行(您将在下面看到) 我以这
这是我之前的问题 you can find it right there 之后的一个问题 所以。现在我定义了一个 DataGrid带有特定的 ElementStyle对于每一列(它只是定义了 Text
我正在尝试检索当前 DataGridCell 对象的行索引。如果它是对角线单元格,我需要它来更改单元格的背景颜色。 我尝试了如下所示的两个选项,这两个选项都为我提供了行索引。但是当我尝试移动滚动条(当
如果单元格中的内容不消失,我似乎无法将工具提示绑定(bind)到 DataGridCell 的内容。换句话说,当我将鼠标悬停在单元格上时,工具提示会按预期出现,但单元格内容不再可见(永久可见,除非我向
我有一个 WPF DataGrid,它显示实现 IDataErrorInfo 的类型。正如预期的那样,当验证失败时,该行会显示红色感叹号,无效单元格会显示红色突出显示。 这一切都很好;但是,我希望验证
我在我的应用程序中使用 MahApps 库,datagrid 单元格的默认样式没有任何边框: 但我希望 DataGrid 看起来像这样: 所以我开始了:
我正在尝试根据其值有条件地控制列中的 DataGrid 单元格背景。不幸的是我得到这样的东西: 这不是很美观,我希望整个单元格都具有不同的颜色,而不仅仅是文本后面的部分。这是代码部分:
我知道我的问题已经在这里以多种方式陈述和回答。但我就是无法让它按照我想要的方式运行。如标题所述,我尝试根据其内容更改 datagridcell 的背景颜色。我对 WPF 比较陌生,但我想解决方案是将转
我有一个带有 4 列的 wpf 数据网格控件。所有这 4 列都绑定(bind)了 4 个 decimal 类型的属性。所以它出现在网格单元格中,如“43.00000”、“456.000000”等。它如
基本上,我绑定(bind)了数据网格,使其类似于科目时间表 - 每行代表一个学期的科目,该学期内的每个单元格代表一个科目。 我现在正在尝试添加拖放功能,以便您可以将其他主题拖到网格上,这将更新底层数据
假设我在 DataGridTemplateColumn 中有一个任意控件,我想知道如何获取该控件,因为我已检索到包含该控件的 DataGridCell。 我的包含 DataGrid 的 XAML 文件
问题是: 当我们要编辑一个DataGridCell时,我们必须首先选择DataGridRow。我的意思是要在 DataGridRow.Current 之外编辑 DataGridCell,我们需要 1.
我对 WPF 中的数据绑定(bind) DataGrid 有疑问。我使用的是 VS 2010 Beta 2,它有自己的 DataGrid,而不是 Toolkit,虽然我认为它几乎相同。 我想绑定(bi
我正在尝试设置 DataGridCells 的 Validation.ErrorTemplate,这是 xaml 代码:
当我们禁用( IsEnabled="False" )DataGridCell 时,它停止引发鼠标双击事件。当单元格被双击时(在禁用和启用状态下),我们仍然希望能够利用双击事件来做一些事情。 我们怎么做
DataGridColumnHeader.BorderThickness=0 对我有用,但不适用于 DataGridRow 或 DataGridCell,有什么想法吗?
我是一名优秀的程序员,十分优秀!