如何通过引用传递真正在C#中工作?

时间:2021-10-03 19:04:10

I am wondering how passing by reference works in C#. If I pass a WPF DataGrid control by reference to a function, and slowly have the function add items to its DataGrid.Items collection, will the DataGrid in the UI update with each new DataGrid.Items.Add() call? Or will it only update when the function returns? I would like to achieve the former scenario, so if passing by reference doesn't do the trick, I would love some advice on how to accomplish this. Thanks in advance.

我想知道如何通过引用传递C#。如果我通过引用函数传递WPF DataGrid控件,并慢慢地将函数添加到其DataGrid.Items集合中,那么UI中的DataGrid会更新每个新的DataGrid.Items.Add()调用吗?或者它只会在函数返回时更新?我想实现前一个场景,所以如果通过引用传递不起作用,我会喜欢一些关于如何实现这一点的建议。提前致谢。

3 个解决方案

#1


2  

if u want the former scenario, then call the function UpdateLayout() of datagrid in the next statement after you use Add() method. Hope this will help.

如果你想要前一个场景,那么在使用Add()方法之后,在下一个语句中调用datagrid的函数UpdateLayout()。希望这会有所帮助。

#2


2  

Passing by reference is irrelevant to your situation anyway. Passing by reference (i.e. ref) allows you to assign to the variable in the calling scope. You are not trying to do that. You only want to access the DataGrid reference and access its Items field. That does not require ref at all.

无论如何,通过引用传递与您的情况无关。通过引用传递(即ref)允许您分配调用范围中的变量。你不是想这样做。您只想访问DataGrid引用并访问其Items字段。这根本不需要参考。

#3


0  

Just as an extension to what Ethicallogics said I would subclass and override add so that it calls UpdateLayout each time, then you don't have to worry about it.

正如Ethicallogics所说的那样,我将子类化并覆盖add以使其每次都调用UpdateLayout,然后您不必担心它。

In general it's good to subclass all the .net classes [specifically for anything UI related] you will need, that way if you run into custom requirements you don't have to do it later. Use your own subclasses at all times.

一般来说,将所有.net类(特别是与UI相关的所有类)子类化是一件好事,如果您遇到自定义要求,则不必在以后执行此操作。始终使用您自己的子类。

#1


2  

if u want the former scenario, then call the function UpdateLayout() of datagrid in the next statement after you use Add() method. Hope this will help.

如果你想要前一个场景,那么在使用Add()方法之后,在下一个语句中调用datagrid的函数UpdateLayout()。希望这会有所帮助。

#2


2  

Passing by reference is irrelevant to your situation anyway. Passing by reference (i.e. ref) allows you to assign to the variable in the calling scope. You are not trying to do that. You only want to access the DataGrid reference and access its Items field. That does not require ref at all.

无论如何,通过引用传递与您的情况无关。通过引用传递(即ref)允许您分配调用范围中的变量。你不是想这样做。您只想访问DataGrid引用并访问其Items字段。这根本不需要参考。

#3


0  

Just as an extension to what Ethicallogics said I would subclass and override add so that it calls UpdateLayout each time, then you don't have to worry about it.

正如Ethicallogics所说的那样,我将子类化并覆盖add以使其每次都调用UpdateLayout,然后您不必担心它。

In general it's good to subclass all the .net classes [specifically for anything UI related] you will need, that way if you run into custom requirements you don't have to do it later. Use your own subclasses at all times.

一般来说,将所有.net类(特别是与UI相关的所有类)子类化是一件好事,如果您遇到自定义要求,则不必在以后执行此操作。始终使用您自己的子类。