How to tell Size of a Grid Cell?
I have a custom control that I've placed inside a cell of a grid. I have开发者_StackOverflow some internal calculations I need to run based on the height and width of the custom control. However, I want it to resize based on the size of the cell.
So, my main question is... how to I programatically determine the height and width of a given cell?
It may be easier to figure out the Height and Width of your Custom Control rather than the cell of a DataGrid.
Your control should have access (through FrameWork Element) to the properties ActualHeight and ActualWidth. These properties will update when the size changes.
Also, the SizedChanged event will be fired on your control every time the Height and Width change.
I highly recommend placing this height and width logic inside of your control. You do not want to be limited to only placing your custom control inside of DataGrids.
private void UserControl_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
HeightBox.Text = this.ActualHeight.ToString();
}
You can get size of grid cell through RowDefinitions and ColumnDefinitions:
MyGrid.RowDefinitions[1].ActualHeight
MyGrid.ColumnDefinitions[1].ActualWidth
but it should be pretty rare situation then you have to resort to this kind of approach
精彩评论