简体   繁体   English

为什么有些属性无法访问

[英]Why are some properties unaccessable

I was trying to get the text that I wrote in a DataGrid cell after editing it, so I put a breakpoint in the function CellEditEnding and looked at the EventArgs and noticed that it contains the property "Text", so I wouldn't have to do the usual XAML binding hacks to get it.我试图获取编辑后在 DataGrid 单元格中写入的文本,因此我在 function CellEditEnding 中放置了一个断点并查看了 EventArgs 并注意到它包含属性“文本”,因此我不必做通常的 XAML 绑定黑客来获得它。

文本属性

However, I quickly noticed that it will not let me access it.但是,我很快注意到它不会让我访问它。 尝试访问 Text 属性时出错

After taking a look at the FrameworkElement class, I can confirm that there is no Text property, so what is going on, why can't I acces the property?看了一下FrameworkElement class,可以确定没有Text属性,这是怎么回事,为什么不能访问该属性?

why can't I acces the property?为什么我不能访问该属性?

Because a FrameworkElement indeed has no Text property.因为FrameworkElement确实没有Text属性。

TextBox , which derives from FrameworkElement , has a Text property though so you could cast the EditingElement to a TextBox and then access the property:派生自FrameworkElementTextBox具有Text属性,因此您可以将EditingElement转换为TextBox然后访问该属性:

string text = (e.EditingElement as TextBox)?.Text;

Visual Studio displays the properties of the actual object in memory. Visual Studio 在 memory 中显示实际 object 的属性。

It's a bad idea to use the UI as a data store and try and directly work with it.将 UI 用作数据存储并尝试直接使用它不是一个好主意。

You should bind an observablecollection of t to the itemssource of your datagrid and work with each instance of t.您应该将 t 的 observablecollection 绑定到数据网格的 itemssource 并使用 t 的每个实例。

That will be far easier to work with.这将更容易使用。

As to why are some properties inaccessible?至于为什么有些属性无法访问?

It's because those properties aren't where you think they are.这是因为这些属性并不在您认为的位置。 The DatagridCell has a series of things nested within it. DatagridCell 中嵌套了一系列内容。

DataGridCell > Border > ContentPresenter > TextBlock DataGridCell > 边框 > ContentPresenter > TextBlock

Download snoop https://github.com/snoopwpf/snoopwpf or install using chocolatey / your preferred method.下载 snoop https://github.com/snoopwpf/snoopwpf或使用 chocolatey / 你喜欢的方法安装。

Run snoop.运行窥探。

Run your app.运行您的应用程序。

Drag the right gunsight thing over you window.将正确的瞄准具拖到你身上 window。

A window should open up with two panels.一个 window 应该打开两个面板。 Controls and properties.控件和属性。

Mouse over a datagrid cell.将鼠标悬停在数据网格单元格上。

press shift+ctrl and you should see the element under the mouse selected in the ui tree.按 shift+ctrl,您应该会在 ui 树中看到鼠标下选中的元素。

A datagrid is pretty complicated and there are multiple things in each row.数据网格非常复杂,每一行中都有多个内容。

在此处输入图像描述

See that textblock there?看到那个文本块了吗?

That's the thing has a text property.那就是具有文本属性的东西。

Or at least that's the thing when you're not in edit mode.或者至少当您不处于编辑模式时就是这样。

Switch to edit mode and I have a TextBoxView.切换到编辑模式,我有一个 TextBoxView。

So one complication is, which are you working with at a given time?所以一个复杂的问题是,你在给定的时间和谁一起工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM