简体   繁体   中英

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.

文本属性

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?

why can't I acces the property?

Because a FrameworkElement indeed has no Text property.

TextBox , which derives from FrameworkElement , has a Text property though so you could cast the EditingElement to a TextBox and then access the property:

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

Visual Studio displays the properties of the actual object in memory.

It's a bad idea to use the UI as a data store and try and directly work with it.

You should bind an observablecollection of t to the itemssource of your datagrid and work with each instance of 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 > Border > ContentPresenter > TextBlock

Download snoop https://github.com/snoopwpf/snoopwpf or install using chocolatey / your preferred method.

Run snoop.

Run your app.

Drag the right gunsight thing over you window.

A window should open up with two panels. 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.

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.

So one complication is, which are you working with at a given time?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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