I need to access the text in a DataGrid
's template column from code behind, but I don't know how. I need to change the text to whatever string I pass to it on the SelectionChanged
event. Can someone please tell me how to achieve this? I found a similar question here
but it had no answers.
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
To find a control in a
DataGrid
template column, you should useFindChild()
:For example I have this template column in MyDataGrid:
Access it from the code, you can:
Note: Always use FindChild only when the control will be fully loaded, otherwise it will not find it and give null. In this case, I put this code in the event ContentRendered (Window) which says that all the contents of the window successfully load (even the event MyDataGrid_Loaded not have access to MyTextBlock, because it is not yet loaded):
EDIT1:
To access the control of selected row to add event SelectionChanged to DataGrid in which to function, which will give a selected row:
Listing of GetDataGridRows():
EDIT2:
To get ALL the items I rewrote the function FindChild():
Calling this new function:
Generally, this practice is not the best ... to get the items (such as all or selected), you can contact directly to the list which stores your data (like ObservableCollection). Also, it is useful events such as PropertyChanged.