Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as I know there is no event that is raised when you resize a row's height.</p> <p>My first suggestion would be to set the RowStyle in order to create a binding (OneWay) between the the DataGridRow's height property and the datagrid's RowHeight property, but if you check the Row's height after you resize it, it is unchanged, the ActualHeight is the property that contains the row's "actual" height when you resize it, and ActualHeight cannot be set because "it does not have an accessible set accessor".</p> <p>After trying this I thought: Where does DataGridRow's ActualHeight gets its value from? </p> <p>I remembered this <a href="http://wpfadventures.wordpress.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row/" rel="nofollow noreferrer">post</a> that explains how to detect which cell and row got clicked and also shows the DataGrid's default template visual tree.</p> <p>By trial and error (using the visual tree image in the link above) I found that it was DataGridCellPresenter that stored the Height that was being used (actually I'm not 100% sure about this, it was the first class that had the height changed up the visual tree since DataGridCell).</p> <p>Apparently DataGrid doesn't expose the API to get the DataGridCellsPresenter from a DataGridRow (as I found out <a href="http://wpf.codeplex.com/Thread/View.aspx?ThreadId=51143" rel="nofollow noreferrer">here</a>)</p> <p>So my first approach was to get all the DataGridCellPresenter in the DataGrid (through the visual tree) after it has been populated and programatically create a binding between the Height property of the DataGridPresenter and the RowHeight property of the DataGrid.</p> <p>Here's the code to do that (my DataGrid's instance name is dataGrid1):</p> <p>Getting all the DataGridCellPresenter:</p> <pre><code> void GetAllDataGridCellPresenters(DependencyObject parent, List&lt;DataGridCellsPresenter&gt; presenters) { int numberOfChildren = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i &lt; numberOfChildren; i++) { if (VisualTreeHelper.GetChild(parent, i) is DataGridCellsPresenter) { presenters.Add(VisualTreeHelper.GetChild(parent, i) as DataGridCellsPresenter); } else if (VisualTreeHelper.GetChild(parent, i) != null) { GetAllDataGridCellPresenters(VisualTreeHelper.GetChild(parent, i), presenters); } else return; } } </code></pre> <p>Setting the bindings programatically on all of them (call this when the Loaded event is raised by the DataGrid):</p> <pre><code> void SetBindingInDataGridPresenter() { List&lt;DataGridCellsPresenter&gt; presenters = new List&lt;DataGridCellsPresenter&gt;(); GetAllDataGridCellPresenters(dataGrid1, presenters); foreach (DataGridCellsPresenter presenter in presenters) { Binding binding = new Binding("RowHeight"); binding.Source = dataGrid1; binding.Mode = BindingMode.TwoWay; presenter.SetBinding(DataGridCellsPresenter.HeightProperty, binding); } } </code></pre> <p>(Note: Setting the binding as OneWayToSource didn't work, I really don't know why, I'm probably missing something obvious here...)</p> <p>This did work... sort of... because I used the Visual Tree to get the DataGridCellsPresenter I only got the visible ones :P, but this shows it can be done this way.</p> <p>So, finally, the right way to do it would be to supply the DataGrid control template, it can be just as the default one except with the DataGridCellsPresenter's Height property data bound to the RowHeight property of the DataGrid.</p> <p>I know this does not show exactly how to do it, but you just have to learn (so do I :P) to <a href="http://blogs.msdn.com/jitghosh/archive/2007/12/27/wpf-control-templates-an-overview.aspx" rel="nofollow noreferrer">redefine a control's template</a>; somehow get the default DataGrid template (or if you're already using another then great, you probably know more than me about it and already know how to do it in order to get the DataGridCellsPresenter Height property automatically bound to the RowHeight DataGrid property) and change it with that bit of magic that gets both height properties bound.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload