I was messing around with DataGrid
headers and discovered something awkward. I toyed with it until I found the root cause in the XAML and built a small sample of how to reproduce.
I only tested this with textbox and datagrid, but I suspect it works with other controls as well. Setting the Margin
property ( I set Margin="2"
) on either the datagrid or the textbox when their widths are bound to a sibling element through ActualWidth
will cause my entire Visual Studio to stop responding almost immediately.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" x:Name="panel">
<TextBlock Text="Text:1" Width="90" />
<TextBox Width="90" />
</StackPanel>
<DataGrid AutoGenerateColumns="False" Width="{Binding ActualWidth, ElementName=panel}">
<DataGrid.Columns>
<DataGridTextColumn Header="Test" Width="*" />
<DataGridTextColumn Header="Test2" Width="*" />
<DataGridTextColumn Header="Test3" Width="*" />
</DataGrid.Columns>
</DataGrid>
<TextBox Width="{Binding ActualWidth, ElementName=panel}" />
</StackPanel>
</Window>
This is what the design preview looks before it stops responding. The window stretches into "infinity" until what I suspect to be VS running out of memory.
Any idea what could be causing this?
Try set
HorizontalAlignment="Left"
(it does not matter what kind of alignment) forStackPanel
:At the panel width will be
180
, and all the rest controls, this value will inherit from binding (DataGrid
,TextBox
).Quote from StackPanel MSDN:
Since the default alignment
NaN
of panel, that panel settingsWidth
andHeight
are inherited from parent -StackPanel
, who also has no explicit parameters. And in this panel, the width and height inherits from theWindow
.