WPF with datagrid

WPF with datagrid

sonicer
Collaborator Collaborator
1,079 Views
2 Replies
Message 1 of 3

WPF with datagrid

sonicer
Collaborator
Collaborator

Hello,

I am starting change Winform to WPF.

I have datagrid with abou 50 fields from Revit.


But I can't read some field currently invisible in datagrid. (visible when I scroll down)

 

TextBlock item= datagrid1.Columns[2].GetCellContent(datagrid1.Items[15]) as TextBlock;
            MessageBox.Show(item.Text);

this make error in revit...

but if I scroll to item 15 and click - it work correctly.

 

thanks..

0 Likes
Accepted solutions (1)
1,080 Views
2 Replies
Replies (2)
Message 2 of 3

jlpgy
Advocate
Advocate
Accepted solution

Hi:

Here is my simple conclusion:

Your code is not MVVM.

 

Let's suppose that you have the cell that you are visiting has not yet been displayed on the screen (not yet rendered).

In WPF DataGrid control ( as well as most other controls), data won't be prepared before rendering.

Think abt it in another way. Bind DataGrid.ItemsSource property to a IEnumerable<T> collection property in your view model, then each single item has already been prepared whether the DataGrid control having been rendered or not.

The spirit of MVVM is that both the view and the view model can run without knowing each other.

Which means that you should *NEVER* visit DataGridCell.Content to get a TextBox.Text data. You should directly visit the collection property in you view model.

It's a gap for developers from WinForm 🙂

单身狗;代码狗;健身狗;jolinpiggy@hotmail.com
Message 3 of 3

sonicer
Collaborator
Collaborator

thanks much

...thats look like as web system Joomla or Wordpress.

 

It so different Winform and WPF.